标签:add under center lock rom spfa strong cannot hdoj
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2415 Accepted Submission(s): 765
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXN = 400005; const int INF = 0x3f3f3f3f; struct Edge{ int to, w, net; }es[4000005]; int head[MAXN], tot; int n, m; void addedge(int u, int v, int w) { es[tot].to = v; es[tot].w = w; es[tot].net = head[u]; head[u] = tot++; } void spfa(int src, int d[], bool vis[], int n) { for(int i = 1; i <= n; i++) { d[i] = INF; vis[i] =false; } d[src] = 0; queue<int> que; que.push(src); while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for(int i = head[u]; i != -1; i = es[i].net) { Edge e = es[i]; if(d[e.to] > d[u] + e.w) { d[e.to] = d[u] + e.w; if(!vis[e.to]) { vis[e.to] = true; que.push(e.to); } } } } } int d[2][MAXN]; bool vis[MAXN]; int vec[MAXN], len; int main() { int T; scanf("%d", &T); for(int cas = 1; cas <= T; cas++) { memset(head, -1, sizeof(head)); tot = 0; scanf("%d %d", &n ,&m); int newN = n; for(int i = 0; i < m; i++) { int w, s; scanf("%d %d", &w, &s); //巧妙构图 int u = ++newN; int v = ++newN; addedge(u, v, w); for(int j = 0; j < s; j++) { int x; scanf("%d", &x); addedge(x, u, 0); addedge(v, x, 0); } } spfa(1, d[0], vis, newN); spfa(n, d[1], vis, newN); int mn = INF; for(int i = 1; i <= n; i++) { int mx = max(d[0][i], d[1][i]); if(mn > mx) { mn = mx; } } if(mn == INF) { printf("Case #%d: Evil John\n", cas); continue; } len = 0; for(int i = 1; i <= n; i++) { if(max(d[0][i], d[1][i]) == mn) { vec[len++] = i; } } printf("Case #%d: %d\n", cas, mn); for(int i = 0; i < len -1; i++) { printf("%d ", vec[i]); } printf("%d\n", vec[len-1]); } return 0; }
标签:add under center lock rom spfa strong cannot hdoj
原文地址:http://www.cnblogs.com/program-ccc/p/6020281.html