hdu 2066 多起点 多终点
多起点 多终点 无向图 结点的个数要自己求
Sample Input
6 2 3 //边数 起点数 终点数
1 3 5 //u v w
1 4 7
2 8 12
3 8 4
4 9 12
9 10 2
1 2 //起点
8 9 10 //终点
Sample Output
9
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # define LL long long 7 using namespace std ; 8 9 const int MAXN=1010; 10 const int INF=0x3f3f3f3f; 11 int n ; 12 bool vis[MAXN]; 13 int cost[MAXN][MAXN] ; 14 int lowcost[MAXN] ; 15 int pre[MAXN]; 16 void Dijkstra(int beg) 17 { 18 for(int i=0;i<n;i++) 19 { 20 lowcost[i]=INF;vis[i]=false;pre[i]=-1; 21 } 22 lowcost[beg]=0; 23 for(int j=0;j<n;j++) 24 { 25 int k=-1; 26 int Min=INF; 27 for(int i=0;i<n;i++) 28 if(!vis[i]&&lowcost[i]<Min) 29 { 30 Min=lowcost[i]; 31 k=i; 32 } 33 if(k==-1)break; 34 vis[k]=true; 35 for(int i=0;i<n;i++) 36 if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]) 37 { 38 lowcost[i]=lowcost[k]+cost[k][i]; 39 pre[i]=k; 40 } 41 } 42 } 43 44 int main () 45 { 46 //freopen("in.txt","r",stdin) ; 47 int m , s , e; 48 while (scanf("%d %d %d" , &m , &s , &e) !=EOF) 49 { 50 int u , v , w ; 51 int i , j , t = -1 ; 52 int a[MAXN] ; 53 int b[MAXN] ; 54 memset(cost, INF, sizeof(cost)); 55 while(m--) 56 { 57 scanf("%d %d %d" , &u , &v , &w) ; 58 if (w < cost[u-1][v-1] ) 59 { 60 cost[u-1][v-1] = w ; 61 cost[v-1][u-1] = w ; 62 } 63 64 if (u > t) 65 t = u ; 66 if (v > t) 67 t = v ; 68 } 69 n = t ; 70 for (i = 0 ; i < s ; i++) 71 scanf("%d" , &a[i]) ; 72 for (i = 0 ; i < e ; i++) 73 scanf("%d" , &b[i]) ; 74 int ans = INF ; 75 for (i = 0 ; i < s ; i++) 76 { 77 Dijkstra(a[i]-1) ; 78 for (j = 0 ; j < e ; j++) 79 { 80 if (lowcost[b[j]-1] < ans) 81 ans = lowcost[b[j]-1] ; 82 } 83 84 } 85 printf("%d\n" , ans) ; 86 } 87 88 return 0 ; 89 }
堆优化
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6 # include <queue> 7 # define LL long long 8 using namespace std ; 9 10 const int INF=0x3f3f3f3f; 11 const int MAXN=1110; 12 struct qnode 13 { 14 int v; 15 int c; 16 qnode(int _v=0,int _c=0):v(_v),c(_c){} 17 bool operator <(const qnode &r)const 18 { 19 return c>r.c; 20 } 21 }; 22 struct Edge 23 { 24 int v,cost; 25 Edge(int _v=0,int _cost=0):v(_v),cost(_cost){} 26 }; 27 vector<Edge>E[MAXN]; 28 bool vis[MAXN]; 29 int dist[MAXN]; 30 int n ; 31 void Dijkstra(int start)//点的编号从1开始 32 { 33 memset(vis,false,sizeof(vis)); 34 for(int i=1;i<=n;i++)dist[i]=INF; 35 priority_queue<qnode>que; 36 while(!que.empty())que.pop(); 37 dist[start]=0; 38 que.push(qnode(start,0)); 39 qnode tmp; 40 while(!que.empty()) 41 { 42 tmp=que.top(); 43 que.pop(); 44 int u=tmp.v; 45 if(vis[u])continue; 46 vis[u]=true; 47 for(int i=0;i<E[u].size();i++) 48 { 49 int v=E[tmp.v][i].v; 50 int cost=E[u][i].cost; 51 if(!vis[v]&&dist[v]>dist[u]+cost) 52 { 53 dist[v]=dist[u]+cost; 54 que.push(qnode(v,dist[v])); 55 } 56 } 57 } 58 } 59 void addedge(int u,int v,int w) 60 { 61 E[u].push_back(Edge(v,w)); 62 } 63 64 int main () 65 { 66 // freopen("in.txt","r",stdin) ; 67 int m , s , e; 68 while (scanf("%d %d %d" , &m , &s , &e) !=EOF) 69 { 70 71 int u , v , w ; 72 int i , j ; 73 int a[MAXN] ; 74 int b[MAXN] ; 75 for(i=1;i<=MAXN;i++) 76 E[i].clear(); 77 n = 0 ; 78 while(m--) 79 { 80 scanf("%d%d%d" , &u , &v , &w) ; 81 addedge(u,v,w) ; 82 addedge(v,u,w) ; 83 n = max(n,max(u,v)) ; 84 } 85 for (i = 0 ; i < s ; i++) 86 scanf("%d" , &a[i]) ; 87 for (i = 0 ; i < e ; i++) 88 scanf("%d" , &b[i]) ; 89 int ans = INF ; 90 for (i = 0 ; i < s ; i++) 91 { 92 Dijkstra(a[i]) ; 93 for (j = 0 ; j < e ; j++) 94 { 95 if (dist[b[j]] < ans) 96 ans = dist[b[j]] ; 97 } 98 99 } 100 printf("%d\n" , ans) ; 101 102 } 103 104 return 0 ; 105 }