题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521
给n个点,m个块。块内点到点之间话费的时间ti。两个人分别从点1和点n出发,问两人是否可以相遇,并求出相遇最短时间和路径,路径按照字典序输出。
这题的难点在于处理块内的点到块外点的关系。我们可以添加一个“集合点”,此点到集合内各点距离为w,点到此集合的距离为0建图。
从1和n分别做一次最短路,找到每一个距离最长的点(即为相遇点),记下长度再枚举两个结果,看一共多少个相遇点
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
typedef pair<int, int> pii;
typedef struct E {
int w;
int v;
E() {}
E(int vv, int ww) : v(vv), w(ww) {}
}E; const int inf = 0x7f7f7f7f;
const int maxn = ; priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<E> e[maxn];
vector<int> path;
ll d[][maxn];
int n, m, u, v, w, k; template<int cho>
void dijkstra(int s) {
memset(d[cho], inf, sizeof(d[cho]));
while(!pq.empty()) pq.pop();
d[cho][s] = ;
pq.push(pii(, s));
while(!pq.empty()) {
pii cur = pq.top(); pq.pop();
w = cur.first;
v = cur.second;
if(d[cho][v] < w) continue;
for(int i = ; i < e[v].size(); i++) {
if(d[cho][e[v][i].v] > d[cho][v] + e[v][i].w) {
d[cho][e[v][i].v] = d[cho][v] + e[v][i].w;
pq.push(pii(d[cho][e[v][i].v], e[v][i].v));
}
}
}
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scan_d(T);
while(T--) {
for(int i = ; i < maxn; i++) e[i].clear();
path.clear();
scan_d(n); scan_d(m);
for(int i = ; i <= m; i++) {
scan_d(w); scan_d(k);
while(k--) {
scanf("%d", &v);
e[n+i].push_back(E(v, w));
e[v].push_back(E(n+i, ));
}
}
dijkstra<>(); dijkstra<>(n);
ll cur = inf;
for(int i = ; i <= n; i++) {
cur = min(cur, max(d[][i], d[][i]));
}
for(int i = ; i <= n; i++) {
if(cur == max(d[][i], d[][i])) {
path.push_back(i);
}
}
printf("Case #%d: ", _++);
if(cur == inf) {
printf("Evil John\n");
continue;
}
printf("%I64d\n", cur);
for(int i = ; i < path.size(); i++) {
printf("%d", path[i]);
if(i == path.size() - ) printf("\n");
else printf(" ");
}
}
return ;
}