Meeting HDU - 5521 虚点建图

时间:2023-03-10 01:00:58
Meeting HDU - 5521 虚点建图
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.
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.

巧妙的建图 
题意:
       两个人分别在1和n区。给出区之间有联系的图以及到达所需时间。
      求两个人见面最短时间以及在哪个区碰面(可有多个)
巧妙的建图,对于每一个集合里面的点都连向一个虚点,
每一条边的权值为都为tt 最后除以2就好了。
然后分别从1和n跑一遍最短路
minn = min ( minn, max ( d[i][0], d[i][1] ) );
 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define pi acos(-1.0)
#define eps 1e-6
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("in.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x7fffffff;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const int maxn = 3e6 + ;
int t, n, m, tot, head[maxn], vis[maxn];
LL d[maxn][];
struct Edge {
int v, nxt;
LL w;
} edge[maxn * ];
void init() {
tot = ;
mem ( head, - );
}
void add ( int u, int v, LL w ) {
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
}
struct node {
LL d, v;
node ( LL d, LL v ) : d ( d ), v ( v ) {}
bool operator < ( const node & a ) const {
return d > a.d;
}
};
void dijstar ( int flag ) {
priority_queue<node>q;
int s = flag ? n : ;
for ( int i = ; i <= n + m ; i++ ) d[i][flag] = INFLL, vis[i] = ;
d[s][flag] = ;
q.push ( node ( , s ) );
while ( !q.empty() ) {
node temp = q.top();
q.pop();
int u = temp.v;
if ( vis[u] ) continue;
vis[u] = ;
for ( int i = head[u]; ~i ; i = edge[i].nxt ) {
int v = edge[i].v;
if ( d[v][flag] > d[u][flag] + edge[i].w && !vis[v] ) {
d[v][flag] = d[u][flag] + edge[i].w;
q.push ( node ( d[v][flag], v ) );
}
}
}
}
int main() {
sf ( t );
int cas = ;
while ( t-- ) {
sff ( n, m );
init();
int p = n;
for ( int i = , s, v, x ; i < m ; i++ ) {
p++;
LL tt;
scanf ( "%lld%d", &tt, &s );
while ( s-- ) {
sf ( x );
add ( p, x, tt );
add ( x, p, tt );
}
}
dijstar ( );
dijstar ( );
LL minn = INFLL;
vector<int>ans;
for ( int i = ; i <= n ; i++ ) minn = min ( minn, max ( d[i][], d[i][] ) );
if ( minn == INFLL ) printf ( "Case #%d: Evil John\n", cas++ );
else {
ans.clear();
printf ( "Case #%d: %lld\n", cas++, minn / );
for ( int i = ; i <= n ; i++ )
if ( max ( d[i][], d[i][] ) == minn ) ans.push_back ( i );
for ( int i = ; i < ans.size(); i++ ) printf ( "%d%c", ans[i], i == ans.size() - ? '\n' : ' ' );
}
}
return ;
}