标签:style blog http color io os ar java for
1 6 2 1 2 10 2 3 10 3 4 10 4 5 10 5 6 10 6 1 10
5
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 INF 0x3f3f3f3f 15 #define pii pair<int,int> 16 using namespace std; 17 const int maxn = 2050; 18 struct arc{ 19 int to,cost,next; 20 arc(int x = 0,int y = 0,int z = -1){ 21 to = x; 22 cost = y; 23 next = z; 24 } 25 }; 26 arc e[maxn*maxn]; 27 int head[maxn],d[maxn],dd[maxn]; 28 int tot,n,m; 29 bool done[maxn]; 30 void add(int u,int v,int w){ 31 e[tot] = arc(v,w,head[u]); 32 head[u] = tot++; 33 e[tot] = arc(u,w,head[v]); 34 head[v] = tot++; 35 } 36 void dijkstra(int s,int *d){ 37 for(int i = 1; i <= n; ++i) done[i] = false; 38 d[s] = 0; 39 priority_queue< pii,vector< pii >,greater< pii > >q; 40 q.push(make_pair(d[s],s)); 41 while(!q.empty()){ 42 int u = q.top().second; 43 q.pop(); 44 if(done[u]) continue; 45 done[u] = true; 46 for(int i = head[u]; ~i; i = e[i].next){ 47 if(d[e[i].to] > d[u] + e[i].cost){ 48 d[e[i].to] = d[u] + e[i].cost; 49 q.push(make_pair(d[e[i].to],e[i].to)); 50 } 51 } 52 } 53 } 54 int main(){ 55 while(~scanf("%d %d",&m,&n)){ 56 int fire[maxn],tmp,u,v,w; 57 bool isfire[maxn] = {false}; 58 memset(head,-1,sizeof(head)); 59 for(int i = tot = 0; i < m; ++i){ 60 scanf("%d",&tmp); 61 fire[i] = tmp; 62 isfire[tmp] = true; 63 } 64 while(~scanf("%d %d %d",&u,&v,&w)) add(u,v,w); 65 for(int i = 0; i <= n; ++i) d[i] = INF; 66 for(int i = 0; i < m; ++i) dijkstra(fire[i],d); 67 int maxv = INF,index = 1; 68 for(int i = 1; i <= n; ++i){ 69 if(isfire[i]) continue; 70 memcpy(dd,d,sizeof(d)); 71 dijkstra(i,dd); 72 int mmxx = 0; 73 for(int k = 1; k <= n; ++k) 74 mmxx = max(mmxx,dd[k]); 75 if(mmxx < maxv){ 76 index = i; 77 maxv = mmxx; 78 } 79 } 80 printf("%d\n",index); 81 } 82 return 0; 83 }
标签:style blog http color io os ar java for
原文地址:http://www.cnblogs.com/crackpotisback/p/4051890.html