Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3757 Accepted Submission(s): 1209
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.
Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意:给定n,m。表示有n个点,m个集合;每个集合开头有两个整数t,s分别表示这个集合中的点可以互相到达且所用时间为t,s表示这个集合有s个点;
思路:对于每个集合建立一个虚点,这个集合中的每个数字到虚点的距离为t,虚点到各数的距离为0;由此建立一个有向图,分别以1点和n点作为源点求最短路;
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int N=1100056;
const int inf=0x3f3f3f3f;
struct edge
{
int v,cost;
};
vector<edge>E[N];
void add_edge(int u,int v,int w)
{
E[u].push_back((edge){v,w});
}
bool vis[N];
int cnt[N];
int dist1[N],dist2[N];
int distt[N],dt[N];
int n,m;
bool SPFA(int start,int n,int dist[])
{
memset(vis,false,sizeof(vis));
for(int i=1; i<=n; i++) dist[i]=inf;
vis[start]=true;
dist[start]=0;
queue<int> que;
while(!que.empty()) que.pop();
que.push(start);
memset(cnt,0,sizeof(cnt));
cnt[start]=1;
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=false;
for(int i=0; i<(int)E[u].size();i++)
{
int v=E[u][i].v;
if(dist[v]>dist[u]+E[u][i].cost)
{
dist[v]=dist[u]+E[u][i].cost;
if(!vis[v])
{
vis[v]=true;
que.push(v);
if(++cnt[v]>n) return false;
}
}
}
}
return true;
}
int main()
{
int t,cas=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n+m+10;i++)
E[i].clear();
for(int i=1; i<=m; i++)
{
int t,s;
scanf("%d%d",&t,&s);
for(int j=0; j<s; j++)
{
int x;
scanf("%d",&x);
add_edge(n+i,x,0);
add_edge(x,n+i,t);
}
}
//for(int i=1;i<=n;i++)
//{
// printf("%d:",i);
// for(int j=0;j<(int)E[i].size();j++)
// printf("~%d ",E[i][j].v);
// printf("\n");
//}
//spfa最短路操作;
SPFA(1,n+m,dist1);
SPFA(n,n+m,dist2);
// for(int i=1;i<=n;i++)
// printf("%d %d\n",dist1[i],dist2[i]);
int minn=inf;
for(int i=1;i<=n;i++)
{
distt[i]=max(dist1[i],dist2[i]);
minn=min(minn,distt[i]);
}
if(minn==inf)
{
printf("Case #%d: Evil John\n",++cas);
continue ;
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(distt[i]==minn)
{
dt[ans++]=i;
}
}
printf("Case #%d: %d\n",++cas,minn);
for(int i=0;i<ans;i++)
{
if(i==0) printf("%d",dt[i]);
else printf(" %d",dt[i]);
}
printf("\n");
}
return 0;
}