HDU_5521_Meeting

Posted edward108

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU_5521_Meeting相关的知识,希望对你有一定的参考价值。

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3704    Accepted Submission(s): 1187


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.
 

 

Source
 
  • 在路径1->n中取一点使得max(time(1->vex),time(n->vex))最小
  • 很容易考虑从1和n分别跑一遍单源最短路得到两个距离数组dis1和disn,然后逐个比较下就可以了
  • 但是这题难点在于给出结点间关系是以set方式给予的,我们如果不能加速自当前点寻找可达点的查找速度就会tle
  • 每个set看似分离,实际上因为set相交的缘故,在不同set中的两个点是可达的
  • 我们可以先对于单个set考虑,可以通过缩点,对于一个set中的点用一个新的点表示,然后用新点和其他set相连
  • 接下来就是考虑怎样建边使得同一个set中的点之间的距离相同
  • 新加的点不会增加两个点之间的距离,自set中一点到新点不应有距离的增加,那么我们建边的时候set内的点到新点距离为0,新点到set内每一个点的距离都相同
  • 通过这样的缩点和建边就可以将原本的set结构等价转换,我们只需要跑两遍最短路即可

 

 

  1 #include <iostream>
  2 #include <string>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <cmath>
  8 #include <vector>
  9 #include <queue>
 10 #include <stack>
 11 #include <set>
 12 #include <map>
 13 using namespace std;
 14 typedef long long           LL ;
 15 typedef unsigned long long ULL ;
 16 const int    maxn = 1e6 + 10   ;
 17 const LL     inf  = 0x3f3f3f3f3f;
 18 const int    npos = -1         ;
 19 const int    mod  = 1e9 + 7    ;
 20 const int    mxx  = 100 + 5    ;
 21 const double eps  = 1e-6       ;
 22 const double PI   = acos(-1.0) ;
 23 
 24 struct node{
 25     LL len;
 26     int i;
 27     bool operator < (const node &r)const{
 28         if(len!=r.len)
 29             return len<r.len;
 30         else
 31             return i<r.i;
 32     }
 33 };
 34 struct enode{
 35     int v;
 36     LL w;
 37     int next;
 38 };
 39 struct qnode{
 40     int u;
 41     LL dis;
 42     bool operator < (const qnode &r)const{
 43         return dis>r.dis;
 44     }
 45 };
 46 enode edge[maxn<<1];
 47 int head[maxn<<1], cnt;
 48 void addedge(int x, int y, LL z){
 49     edge[cnt]=(enode){y,z,head[x]};
 50     head[x]=cnt++;
 51 }
 52 std::vector< node > buf;
 53 int T, n, m, u, v, s, no1, non;
 54 LL w, dis1[maxn], disn[maxn], ans;
 55 bool vis[maxn];
 56 void DIJ(LL *dis, int source){
 57     LL now;
 58     priority_queue< qnode > Q;
 59     qnode temp;
 60     for(int i=0;i<=n+m;i++){
 61         vis[i]=false;
 62         dis[i]=inf;
 63     }
 64     dis[source]=0LL;
 65     Q.push((qnode){source,dis[source]});
 66     while(!Q.empty()){
 67         temp=Q.top();
 68         Q.pop();
 69         if(!vis[temp.u]){
 70             u=temp.u;
 71             now=temp.dis;
 72             vis[u]=true;
 73             for(int i=head[u];i!=-1;i=edge[i].next){
 74                 v=edge[i].v;
 75                 w=edge[i].w;
 76                 if(now+w<=dis[v]){
 77                     dis[v]=now+w;
 78                     Q.push((qnode){v,dis[v]});
 79                 }
 80             }
 81         }
 82     }
 83 }
 84 int main(){
 85     // freopen("in.txt","r",stdin);
 86     // freopen("out.txt","w",stdout);
 87     while(~scanf("%d",&T)){
 88         for(int kase=1;kase<=T;kase++){
 89             no1=0;
 90             non=0;
 91             scanf("%d %d",&n,&m);
 92             cnt=0;
 93             memset(head,-1,sizeof(head));
 94             for(int i=1;i<=m;i++){
 95                 scanf("%lld %d",&w,&s);
 96                 for(int j=1;j<=s;j++){
 97                     scanf("%d",&u);
 98                     if(u==1)no1=1;
 99                     if(u==n)non=1;
100                     v=n+i;
101                     addedge(u,v,0LL);
102                     addedge(v,u,w);
103                 }
104             }
105             printf("Case #%d: ",kase);
106             if(!(no1&&non)){
107                 printf("Evil John\n");
108                 continue;
109             }
110             DIJ(dis1,1);
111             DIJ(disn,n);
112             buf.clear();
113             for(int i=1;i<=n;i++)
114                 buf.push_back((node){max(dis1[i],disn[i]),i});
115             sort(buf.begin(),buf.end());
116             ans=buf[0].len;
117             if(ans==inf){
118                 printf("Evil John\n");
119             }else{
120                 printf("%lld\n",ans);
121                 printf("%d",buf[0].i);
122                 int i=1;
123                 while(i<buf.size() && buf[i].len==ans)
124                     printf(" %d",buf[i++].i);
125                 cout<<endl;
126             }
127         }
128     }
129     return 0;
130 }

 

 

 

以上是关于HDU_5521_Meeting的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5521.Meeting 最短路模板题

HDU 5521 Meeting(虚拟节点+最短路)

hdu 5521 Meeting(最短路)

HDU 5521 Meeting 最短路

HDU - 5521 Meeting

Meeting HDU - 5521