标签:
//去重边
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #define maxint 999999999 5 using namespace std; 6 int min(int x,int y) 7 { 8 return x<y?x:y; 9 } 10 struct node 11 { 12 int v; 13 int w; 14 int next; 15 }edge[100000]; 16 int vis[2000],head[2000],st[2000]; 17 int n,m,index,d,a[2000],set,dis[2000]; 18 struct Node 19 { 20 int point; 21 int dist; 22 bool operator<(const Node x) const 23 { 24 return x.dist<dist; 25 } 26 }; 27 void add(int x,int y,int z) 28 { 29 int i,j; 30 edge[index].v=y; 31 edge[index].w=z; 32 edge[index].next=head[x]; 33 head[x]=index++; 34 } 35 36 void dij(int s) 37 { 38 priority_queue<Node> q; 39 int i,j; 40 for(i=1;i<=set;i++) 41 { 42 dis[i]=maxint; 43 } 44 dis[s]=0; 45 Node cur; 46 cur.point=s; 47 cur.dist=0; 48 q.push(cur); 49 while(!q.empty()) 50 { 51 Node tmp; 52 tmp=q.top(); 53 q.pop(); 54 int u=tmp.point; 55 if(vis[u]) 56 continue; 57 vis[u]=1; 58 for(j=head[u];j!=-1;j=edge[j].next) 59 { 60 if(!vis[edge[j].v]&&dis[edge[j].v]>dis[u]+edge[j].w) 61 { 62 dis[edge[j].v]=dis[u]+edge[j].w; 63 cur.dist=dis[edge[j].v]; 64 cur.point=edge[j].v; 65 q.push(cur); 66 } 67 } 68 } 69 } 70 int main() 71 { 72 int i,j; 73 while(scanf("%d%d%d",&n,&m,&d)!=EOF) 74 { 75 set=0; 76 int flag; 77 memset(head,-1,sizeof(head)); 78 for(i=1;i<=n;i++) 79 { 80 int x,y,z; 81 scanf("%d%d%d",&x,&y,&z); 82 for(flag=0,j=head[x];j!=-1;j=edge[j].next) 83 { 84 if(y == edge[j].v) 85 { 86 if(z < edge[j].w) 87 edge[j].w=z; 88 flag = 1; 89 break; 90 } 91 } 92 if(set<x) 93 set=x; 94 if(set<y) 95 set=y; 96 if(flag)continue; 97 add(x,y,z); 98 add(y,x,z); 99 } 100 for(i=1;i<=m;i++) 101 { 102 scanf("%d",&st[i]); 103 } 104 for(i=1;i<=d;i++) 105 scanf("%d",&a[i]); 106 int ans=maxint; 107 for(i=1;i<=m;i++) 108 { 109 memset(vis,0,sizeof(vis)); 110 dij(st[i]); 111 for(j=1;j<=d;j++) 112 { 113 114 ans=min(ans,dis[a[j]]); 115 } 116 } 117 printf("%d\n",ans); 118 } 119 }
标签:
原文地址:http://www.cnblogs.com/sweat123/p/4483362.html