Meeting(传送门)
Time Limit : 12000/6000ms (Java/Other) Memory Limit : 262144/262144K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 8
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
Bessie lives in the first block while Elsie lives in the
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
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.
[/hint]
Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
题意
给出
n
个顶点和
解题思路
注意以下几点
- 相遇满足条件的位置可能存在多种,都需要输出
- 两个人都可以在某一位置进行等待(这是一个坑点)
然后由于每一个点集重元素的个数比较大,我们就需要增加两个点,在他们之间,即一个点集增加一个点
a
,让所有的点都连接他,代价为
代码
/*除去冗长的头文件*/
const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 2e6 + 5;
const int INF = 0x3f3f3f3f;
int T, tot, n, m, t, s;
int Head[MAXN];
int V[MAXN];
LL d1[MAXN], d2[MAXN];
struct Edge{
int to, nxt, cost;
}E[MAXN << 1];
void init_edge(){
mem(Head, -1);
tot = 0;
}
void add_edge(int u, int v, int cost){
E[tot].to = v;
E[tot].cost = cost;
E[tot].nxt = Head[u];
Head[u] = tot ++;
}
void BFS(int s, LL * d, int len){
priority_queue<PLI, vector<PLI>, greater<PLI> >que;
while(!que.empty()) que.pop();
for(int i = 0;i <= len;i ++){
d[i] = 1e19;
}
d[s] = 0;
que.push(PLI(0, s));
while(!que.empty()){
PLI p = que.top();
que.pop();
int u = p.second;
if(d[u] < p.first) continue;
for(int v = Head[u]; ~v; v = E[v].nxt){
Edge &e = E[v];
if(d[e.to] > d[u] + e.cost){
d[e.to] = d[u] + e.cost;
que.push(PLI(d[e.to], e.to));
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
//FIN;
//FOUT;
#endif
IO_Init();
scanf("%d", &T);
int cas = 1;
while(T --){
int e;
init_edge();
scanf("%d%d", &n, &m);
int xv = n + 1;
for(int i = 0;i < m;i ++){
scanf("%d%d", &t, &s);
for(int j = 0;j < s;j ++){
scanf("%d", &V[j]);
}
for(int j = 0;j < s;j ++){
add_edge(V[j], xv, 0);
}
add_edge(xv, xv + 1, t);
for(int j = 0;j < s;j ++){
add_edge(xv + 1, V[j], 0);
}
xv += 2;
}
BFS(1, d1, xv);
BFS(n, d2, xv);
LL ans = 1e19;
printf("Case #%d: ", cas ++);
for(int i = 1;i <= n;i ++){
ans = min(ans, max(d1[i], d2[i]));
}
if(ans == (LL)1e19){
printf("Evil John\n");
continue;
}
printf("%I64d\n", ans);
bool flag = false;
for(int i = 1;i <= n;i ++){
if(ans == max(d1[i], d2[i])){
if(flag) printf(" ");
printf("%d", i);
flag = true;
}
}
printf("\n");
}
return 0;
}