Time Limit:2000MS | Memory Limit:65536KB | 64bit IO Format:%I64d & %I64u |
Description
You are given a weighted undirected graph. The vertices are enumerated from 1 to n. Your task is to find the shortest path between the vertex 1 and the vertex n.
Input
The first line contains two integers n and m (2 ≤ n ≤ 105, 0 ≤ m ≤ 105), where n is the number of vertices and m is the number of edges. Following m lines contain one edge each in form ai, bi and wi (1 ≤ ai, bi ≤ n, 1 ≤ wi ≤ 106), where ai, bi are edge endpoints and wi is the length of the edge.
It is possible that the graph has loops and multiple edges between pair of vertices.
Output
Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them.
Sample Input
5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1
1 4 3 5
5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1
1 4 3 5
最短路,堆优Dijkstra。这个题本身没什么,就是每次更新最短路的时候维护一个fa(fa[i]表示i的父亲)。第一次写堆优Dijkstra,第一次用C++链表存图。以后就是我的模板了
#include<stack>
#include<cstdio>
#include<cstring>
#include<utility>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3fLL
typedef long long LL;
struct tnode
{
tnode *next;
int from,to;
LL len;
};
typedef tnode* pnode; pnode Chain[100100];
typedef pair<LL,int> pii;
priority_queue<pii,vector<pii>,greater<pii> > heap;
bool vis[100100];
int N,M,pre[100100];
LL dist[100100];
void insert(int from,int to,LL len)
{
pnode p=new tnode;
p->next=Chain[from];
p->from=from;
p->to=to;
p->len=len;
Chain[from]=p;
}
void readdata()
{
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++) Chain[i]=NULL;
for(int i=1;i<=M;i++)
{
int a,b;
LL c;
scanf("%d%d%I64d",&a,&b,&c);
insert(a,b,c); insert(b,a,c);
}
}
void dijkstra()
{
for(int i=2;i<=N;i++) dist[i]=INF; dist[1]=0;
memset(vis,0,sizeof(vis));
heap.push(make_pair(dist[1],1));
while(!heap.empty())
{
int v=heap.top().second; heap.pop();
if(vis[v]) continue; vis[v]=1;
for(pnode p=Chain[v];p;p=p->next)
{
int u=p->to,len=p->len;
if(dist[u]>dist[v]+len)
{
pre[u]=v;
dist[u]=dist[v]+len;
heap.push(make_pair(dist[u],u));
}
}
}
//for(int i=1;i<=N;i++) printf("%I64d ",dist[i]); printf("%I64d\n",~0LL>>2);
stack<int> Stack;
if(dist[N]==INF) printf("-1");
else
{
Stack.push(N);
while(Stack.top()!=1) Stack.push(pre[Stack.top()]);
while(!Stack.empty()) {printf("%d ",Stack.top());Stack.pop();}
}
}
int main()
{
freopen("CF20C.in","r",stdin);
readdata();
dijkstra();
while(1);
return 0;
}