HDU_5521_Meeting

2018-06-17 21:48:08来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)
 
  • 在路径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 }

 

 

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:hihoCoder_1445_后缀自动机二&#183;重复旋律5

下一篇:【luogu 3383】【模板】线性筛素数