https://blog.csdn.net/lwt36/article/details/49535209
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. 集合缩点,添加一个M个源点,源点→点 权为w,点→源点 权为0 1开始跑一遍最短路,n开始跑一遍最短路,枚举相遇的点就可以了
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 1e6 + 10;
typedef long long LL;
int n, m;
int head[M + N], cnt;
struct Edge {
int v, nxt, c;
} edges[(M + N) << 1];
bool add_edge(int u, int v, int c) {
edges[cnt] = (Edge) {v, head[u], c};
head[u] = cnt++;
}
LL dp[2][M + N];
bool done[M + N];
typedef pair<LL, int> P;
void dijkstra(int s, int k) {
priority_queue<P, vector<P>, greater<P> > q;
memset(dp[k], 0x3f, sizeof dp[k]);
memset(done, false, sizeof done);
dp[k][s] = 0; q.push(P(0, s));
while(q.size()) {
int u = q.top().second; q.pop();
done[u] = true;
for(int i = head[u]; ~i; i = edges[i].nxt) {
int v = edges[i].v, c = edges[i].c;
if(!done[v] && dp[k][v] > dp[k][u] + c) {
dp[k][v] = dp[k][u] + c;
q.push(P(dp[k][v], v));
}
}
}
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
int kase = 0;
while(t--) {
scanf("%d%d", &n, &m);
cnt = 0; memset(head, -1, sizeof head);
for(int i = 1; i <= m; ++i) {
int w, k; scanf("%d%d", &w, &k);
for(int j = 1; j <= k; ++j) {
int x; scanf("%d", &x);
add_edge(n + i, x, w);
add_edge(x, n + i, 0);
}
}
dijkstra(1, 0);
dijkstra(n, 1);
LL minv = 1e18;
for(int i = 1; i <= n; ++i)
minv = min(minv, max(dp[0][i], dp[1][i]));
vector<int> ans;
for(int i = 1; i <= n; ++i)
if(minv == max(dp[0][i], dp[1][i])) ans.push_back(i);
if(minv == 1e18) printf("Case #%d: Evil John\n", ++kase);
else {
printf("Case #%d: %I64d\n", ++kase, minv);
for(int i = 0; i < ans.size(); ++i)
printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
}
}
return 0;
}