HDU5521-最短路
Posted zzq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU5521-最短路相关的知识,希望对你有一定的参考价值。
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4542 Accepted Submission(s): 1436
Problem Description
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
fences they were separated into different blocks. John‘s farm are divided into n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei where Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
fences they were separated into different blocks. John‘s farm are divided into n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei where Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
Input
The first line contains an integer T (1≤T≤6), the number of test cases. Then T test cases
follow.
The first line of input contains n and m. 2≤n≤105. The following m lines describe the sets Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that ∑mi=1Si≤106.
follow.
The first line of input contains n and m. 2≤n≤105. The following m lines describe the sets Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that ∑mi=1Si≤106.
Output
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
Sample Input
2
5 4
1 3 1 2 3
2 2 3 4
10 2 1 5
3 3 3 4 5
3 1
1 2 1 2
Sample Output
Case #1: 3
3 4
Case #2: Evil John
Hint
In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.
最短路,在于建图,如果把每个集合的点都两两建边的话,复杂度爆炸,所以我们对每一个集合建立一个新节点,将集合内的点向这个新节点建边,
权值就是ti,然后跑两次dij枚举每个点找到最小时间和符合最小时间的点。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<queue> 5 #include<algorithm> 6 #include<map> 7 #include<set> 8 #include<vector> 9 #include<functional> 10 using namespace std; 11 #define LL long long 12 #define pli pair<long long,int> 13 #define mp make_pair 14 #define inf 0x7fffffffffffff 15 struct Edge{ 16 int v,w,next; 17 }e[2000010]; 18 int first[1100005]; 19 int tot,n,m; 20 void add(int u,int v,int w){ 21 e[tot].v=v; 22 e[tot].w=w; 23 e[tot].next=first[u]; 24 first[u]=tot++; 25 } 26 bool vis[1100005]; 27 LL d1[1100005],d2[1100005]; 28 void dij(int s,LL d[]){ 29 memset(vis,0,sizeof(vis)); 30 int tot_n=n+m+10; 31 for(int i=0;i<=tot_n;++i) d[i]=inf; 32 priority_queue<pli,vector<pli>,greater<pli> >q; 33 q.push(mp(0,s)); 34 d[s]=0; 35 while(!q.empty()){ 36 int u=q.top().second; 37 q.pop(); 38 if(vis[u]) continue; 39 vis[u]=1; 40 for(int i=first[u];i+1;i=e[i].next){ 41 if(d[e[i].v]>d[u]+e[i].w){ 42 d[e[i].v]=d[u]+e[i].w; 43 q.push(mp(d[e[i].v],e[i].v)); 44 } 45 } 46 } 47 } 48 int main() 49 { 50 int t,i,j,k; 51 int cas=0; 52 cin>>t; 53 while(t--){int ti,si,a; 54 memset(first,-1,sizeof(first)); 55 tot=0; 56 cin>>n>>m; 57 for(i=1;i<=m;++i){ 58 scanf("%d%d",&ti,&si); 59 while(si--){ 60 scanf("%d",&a); 61 add(a,n+i,ti); 62 add(n+i,a,ti); 63 } 64 } 65 printf("Case #%d: ",++cas); 66 dij(1,d1); 67 dij(n,d2); 68 LL mint=inf; 69 for(i=1;i<=n;++i){ 70 mint=min(mint,max(d1[i],d2[i])); 71 } 72 if(mint==inf){ 73 puts("Evil John"); 74 } 75 else{ 76 cout<<mint/2<<endl; 77 for(i=1;i<=n;++i){ 78 if(mint==max(d1[i],d2[i])){ 79 printf("%d",i); 80 break; 81 } 82 } 83 i++; 84 for(;i<=n;++i){ 85 if(mint==max(d1[i],d2[i])){ 86 printf(" %d",i); 87 } 88 } 89 puts(""); 90 } 91 } 92 return 0; 93 }
以上是关于HDU5521-最短路的主要内容,如果未能解决你的问题,请参考以下文章