HDU5521-最短路-建图

时间:2024-10-18 20:33:38

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.
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.

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.

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枚举每个点找到最小时间和符合最小时间的点。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<functional>
using namespace std;
#define LL long long
#define pli pair<long long,int>
#define mp make_pair
#define inf 0x7fffffffffffff
struct Edge{
int v,w,next;
}e[];
int first[];
int tot,n,m;
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
bool vis[];
LL d1[],d2[];
void dij(int s,LL d[]){
memset(vis,,sizeof(vis));
int tot_n=n+m+;
for(int i=;i<=tot_n;++i) d[i]=inf;
priority_queue<pli,vector<pli>,greater<pli> >q;
q.push(mp(,s));
d[s]=;
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(d[e[i].v]>d[u]+e[i].w){
d[e[i].v]=d[u]+e[i].w;
q.push(mp(d[e[i].v],e[i].v));
}
}
}
}
int main()
{
int t,i,j,k;
int cas=;
cin>>t;
while(t--){int ti,si,a;
memset(first,-,sizeof(first));
tot=;
cin>>n>>m;
for(i=;i<=m;++i){
scanf("%d%d",&ti,&si);
while(si--){
scanf("%d",&a);
add(a,n+i,ti);
add(n+i,a,ti);
}
}
printf("Case #%d: ",++cas);
dij(,d1);
dij(n,d2);
LL mint=inf;
for(i=;i<=n;++i){
mint=min(mint,max(d1[i],d2[i]));
}
if(mint==inf){
puts("Evil John");
}
else{
cout<<mint/<<endl;
for(i=;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf("%d",i);
break;
}
}
i++;
for(;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf(" %d",i);
}
}
puts("");
}
}
return ;
}