Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3674 Accepted Submission(s): 1177
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 JohnHintIn 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
题意:给N个点,M个点集合,每个集合内的点可互达,需要ti时间,有一个人在1,另一个人在n,他们可以同时移动,问在哪些点见面可以最短时间。
思路:集合内的点数量较多,两两建边不现实,那么将每个集合i新增一个中转点n+i,一进一出两条边各占一半时间,最后跑两次最短路,遍历点取min值即可。
# include <iostream>
# include <cstring>
# include <cstdio>
# include <queue>
# include <cmath>
# define pb push_back
# define mp make_pair
# define A first
# define B second
# define pii pair<int,double>
using namespace std;
typedef long long LL;
const int N = 2e6;
const double INF = 0x3f3f3f3f3f3f3f3f;
double dis[N], dis2[N];
vector<pii>g[N];
queue<int>qe;
bool vis[N];
int t, T, n, m, p, q;
void spfa(int st, double dis[])
{
while(!qe.empty()) qe.pop();
for(int i=1; i<=n+m; ++i) dis[i] = INF, vis[i] = false;
dis[st] = 0;
qe.push(st);
while(!qe.empty())
{
int u = qe.front();
qe.pop();
vis[u] = false;
for(auto j : g[u])
{
int to = j.A;
double c = j.B;
if(dis[to] > dis[u]+c)
{
dis[to] = dis[u] + c;
if(!vis[to])
{
vis[to] = true;
qe.push(to);
}
}
}
}
}
int main()
{
int cas = 1;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n+m; ++i) g[i].clear();
for(int i=1; i<=m; ++i)
{
scanf("%d%d",&t,&p);
while(p--)
{
scanf("%d",&q);
g[i+n].pb(mp(q,t*1.0/2));
g[q].pb(mp(i+n, t*1.0/2));
}
}
spfa(1, dis);
double ck = fabs(dis[n]-INF);
if(ck < 0.002)
{
printf("Case #%d: Evil John\n", cas++);
continue;
}
spfa(n, dis2);
double ans = max(dis[1], dis2[1]);
vector<int>v;
for(int i=1; i<=n; ++i)
{
if(max(dis[i],dis2[i]) < ans)
{
ans = max(dis[i], dis2[i]);
v.clear();
v.pb(i);
}
else if(max(dis[i],dis2[i]) == ans)
v.pb(i);
}
printf("Case #%d: %.0f\n",cas++, ans);
for(int i=0; i<v.size(); ++i)
printf("%d%c",v[i],i==v.size()-1?'\n':' ');
}
return 0;
}