Meeting HDU

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 (1im) 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 (1T6), the number of test cases. Then T test cases
follow.

The first line of input contains n and m2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109)and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
 
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] ) );
 
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <set>
  7 #include <iostream>
  8 #include <map>
  9 #include <stack>
 10 #include <string>
 11 #include <vector>
 12 #define  pi acos(-1.0)
 13 #define  eps 1e-6
 14 #define  fi first
 15 #define  se second
 16 #define  lson l,m,rt<<1
 17 #define  rson m+1,r,rt<<1|1
 18 #define  rtl   rt<<1
 19 #define  rtr   rt<<1|1
 20 #define  bug         printf("******
")
 21 #define  mem(a,b)    memset(a,b,sizeof(a))
 22 #define  name2str(x) #x
 23 #define  fuck(x)     cout<<#x" = "<<x<<endl
 24 #define  f(a)        a*a
 25 #define  sf(n)       scanf("%d", &n)
 26 #define  sff(a,b)    scanf("%d %d", &a, &b)
 27 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
 28 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
 29 #define  pf          printf
 30 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
 31 #define  FREE(i,a,b) for(i = a; i >= b; i--)
 32 #define  FRL(i,a,b)  for(i = a; i < b; i++)
 33 #define  FRLL(i,a,b) for(i = a; i > b; i--)
 34 #define  FIN         freopen("in.txt","r",stdin)
 35 #define  gcd(a,b)    __gcd(a,b)
 36 #define  lowbit(x)   x&-x
 37 #pragma  comment (linker,"/STACK:102400000,102400000")
 38 using namespace std;
 39 typedef long long  LL;
 40 typedef unsigned long long ULL;
 41 const int INF = 0x7fffffff;
 42 const LL INFLL = 0x3f3f3f3f3f3f3f3f;
 43 const int mod = 1e9 + 7;
 44 const int maxn = 3e6 + 10;
 45 int t, n, m, tot, head[maxn], vis[maxn];
 46 LL d[maxn][2];
 47 struct Edge {
 48     int v, nxt;
 49     LL w;
 50 } edge[maxn * 10];
 51 void init() {
 52     tot = 0;
 53     mem ( head, -1 );
 54 }
 55 void add ( int u, int v, LL w ) {
 56     edge[tot].v = v;
 57     edge[tot].w = w;
 58     edge[tot].nxt = head[u];
 59     head[u] = tot++;
 60 }
 61 struct node {
 62     LL d, v;
 63     node ( LL d, LL v ) : d ( d ), v ( v ) {}
 64     bool operator < ( const node & a ) const {
 65         return d > a.d;
 66     }
 67 };
 68 void dijstar ( int flag ) {
 69     priority_queue<node>q;
 70     int s = flag ? n : 1;
 71     for ( int i = 0 ; i <= n + m ; i++ ) d[i][flag] = INFLL, vis[i] = 0;
 72     d[s][flag] = 0;
 73     q.push ( node ( 0, s ) );
 74     while ( !q.empty() ) {
 75         node temp = q.top();
 76         q.pop();
 77         int u = temp.v;
 78         if ( vis[u] ) continue;
 79         vis[u] = 1;
 80         for ( int i = head[u]; ~i ; i = edge[i].nxt ) {
 81             int v = edge[i].v;
 82             if ( d[v][flag] > d[u][flag] + edge[i].w && !vis[v] ) {
 83                 d[v][flag] = d[u][flag] + edge[i].w;
 84                 q.push ( node ( d[v][flag], v ) );
 85             }
 86         }
 87     }
 88 }
 89 int main() {
 90     sf ( t );
 91     int cas = 1;
 92     while ( t-- ) {
 93         sff ( n, m );
 94         init();
 95         int p = n;
 96         for ( int i = 0, s, v, x ; i < m ; i++ ) {
 97             p++;
 98             LL tt;
 99             scanf ( "%lld%d", &tt, &s );
100             while ( s-- ) {
101                 sf ( x );
102                 add ( p, x, tt );
103                 add ( x, p, tt );
104             }
105         }
106         dijstar ( 0 );
107         dijstar ( 1 );
108         LL minn = INFLL;
109         vector<int>ans;
110         for ( int i = 1 ; i <= n ; i++ ) minn = min ( minn, max ( d[i][0], d[i][1] ) );
111         if ( minn == INFLL ) printf ( "Case #%d: Evil John
", cas++ );
112         else {
113             ans.clear();
114             printf ( "Case #%d: %lld
", cas++, minn / 2 );
115             for ( int i = 1 ; i <= n ; i++ )
116                 if ( max ( d[i][0], d[i][1] ) == minn ) ans.push_back ( i );
117             for ( int i = 0 ; i < ans.size(); i++ ) printf ( "%d%c", ans[i], i == ans.size() - 1 ? '
' : ' ' );
118         }
119     }
120     return 0;
121 }
原文地址:https://www.cnblogs.com/qldabiaoge/p/9788211.html