Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 81706 Accepted Submission(s): 35385
输入保证至少存在1条商店到赛场的路线。
#include <iostream> #include <cmath> #include <iomanip> #include<queue> #include<algorithm> #include<vector> using namespace std; #define inf 0x3f3f3f3f #define ll long long bool book[105]; struct node { int c; int v; int nxt; }e[20005]; struct node1 { int c; int v; friend bool operator <(node1 x, node1 y) { return x.c > y.c;//x成员小的优先级高,就越排在队首 } }a[20005]; int cnt=1; int head[105]; priority_queue<node1>q; void add(int u,int v,int z) { e[cnt].v=v; e[cnt].c=z; e[cnt].nxt=head[u]; head[u]=cnt++; } int d[105]; int main() { int n,m; while(cin>>n>>m) { if(n==0&&m==0) break; while(!q.empty()) q.pop(); memset(head,-1,sizeof(head)); memset(d,inf,sizeof(d)); memset(book,0,sizeof(book)); for(int i=1;i<=m;i++) { int x,y,z; cin>>x>>y>>z; add(x,y,z); add(y,x,z); } for(int i=head[1];i!=-1;i=e[i].nxt) { node1 x; x.c=e[i].c; x.v=e[i].v; d[x.v]=x.c; book[x.v]=1; q.push(x); } while(!q.empty()) { node1 x; x=q.top(); book[x.v]=0; q.pop(); for(int i=head[x.v];i!=-1;i=e[i].nxt) { if(d[e[i].v]>d[x.v]+e[i].c) { d[e[i].v]=d[x.v]+e[i].c; node1 y; if(!book[e[i].v]) { book[e[i].v]=1; y.v=e[i].v; y.c=d[e[i].v]; q.push(y); } } } } cout<<d[n]<<endl; } return 0; }