标签:des style blog http io color ar os java
1 3 3 1 2 3 1 3 4 2 3 5
Scenario #1: 4
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc{ 19 int to,w,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 w = y; 23 next = z; 24 } 25 }; 26 arc e[200000]; 27 int d[maxn],head[maxn],tot,n,m; 28 bool done[maxn]; 29 void add(int u,int v,int w){ 30 e[tot] = arc(v,w,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,w,head[v]); 33 head[v] = tot++; 34 } 35 void dijkstra(){ 36 for(int i = 1; i <= n; ++i){ 37 d[i] = 0; 38 done[i] = false; 39 } 40 d[1] = INF; 41 priority_queue< pii,vector< pii >,less< pii > >q; 42 q.push(make_pair(0,1)); 43 while(!q.empty()){ 44 int u = q.top().second; 45 q.pop(); 46 if(done[u]) continue; 47 done[u] = true; 48 for(int i = head[u]; ~i; i = e[i].next){ 49 if(d[e[i].to] < min(d[u],e[i].w)){ 50 d[e[i].to] = min(d[u],e[i].w); 51 q.push(make_pair(d[e[i].to],e[i].to)); 52 } 53 } 54 if(done[n]) break; 55 } 56 } 57 int main() { 58 int T,u,v,w,cs = 1; 59 scanf("%d",&T); 60 while(T--){ 61 scanf("%d %d",&n,&m); 62 memset(head,-1,sizeof(head)); 63 for(int i = tot = 0; i < m; ++i){ 64 scanf("%d %d %d",&u,&v,&w); 65 add(u,v,w); 66 } 67 dijkstra(); 68 printf("Scenario #%d:\n%d\n\n",cs++,d[n]); 69 } 70 return 0; 71 }
标签:des style blog http io color ar os java
原文地址:http://www.cnblogs.com/crackpotisback/p/4071148.html