HDU 5521 Meeting
来源:互联网 发布:淘宝夜店装 编辑:程序博客网 时间:2024/06/06 04:04
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 inton blocks labelled from 1 to n .
Bessie lives in the first block while Elsie lives in then -th one. They have a map of the farm
which shows that it takes theyti minutes to travel from a block in Ei to another block
inEi 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.
fences they were separated into different blocks. John's farm are divided into
Bessie lives in the first block while Elsie lives in the
which shows that it takes they
in
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 containsn 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 .
follow.
The first line of input contains
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.
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
25 41 3 1 2 32 2 3 410 2 1 53 3 3 4 53 11 2 1 2
Sample Output
Case #1: 33 4Case #2: Evil John解: 虚拟m个点(不属于1~n),也就是每个集合一个,让每个集合的点与相应的点建立双向关系;两遍spfa.(合理性自己画个图可简单了解)原因:大大减少了建边数,不会超时;说一下 邻接表的head最好不要放在结构体里,自己单独拿出来自成数组,否则赋值-1不方便,而且有些情况会超时;#include <iostream> #include <algorithm> #include <string.h> #include <queue> using namespace std; const long long inf=1e18+10;const long long maxx=1500000;typedef long long ll;int m,n,q; ll dis[maxx];ll di[maxx];ll sto[maxx];ll cc[maxx];int head[maxx];bool vis[maxx]; struct point{ int to; int next; int vaule; }; point pt[2500100]; //注意边数与点数 (题目中有限制)ll minn;void add(int u,int v,int val){ pt[q].next=head[u]; pt[q].to=v; pt[q].vaule=val; head[u]=q++;} void SPFA(int st) { for (int i=0;i<=m+n;i++) dis[i]=inf; queue<int>q; int w,v; memset(vis,false,sizeof(vis)); while (!q.empty())q.pop(); q.push(st); dis[st]=0; vis[st]=true; while (!q.empty()) { v=q.front(); q.pop(); vis[v]=false; for (int i=head[v];i!=-1;i=pt[i].next) { w=pt[i].vaule; if (w+dis[v]<dis[pt[i].to]) { dis[pt[i].to]=w+dis[v]; if (!vis[pt[i].to]) { vis[pt[i].to]=true; q.push(pt[i].to);}}} }} void SPFA2(int st) { for (int i=0;i<=m+n;i++) di[i]=inf; queue<int>q; int w,v; memset(vis,false,sizeof(vis)); while (!q.empty())q.pop(); q.push(st); di[st]=0; vis[st]=true; while (!q.empty()) { v=q.front(); q.pop(); vis[v]=false; for (int i=head[v];i!=-1;i=pt[i].next) { w=pt[i].vaule; if (w+di[v]<di[pt[i].to]) { di[pt[i].to]=w+di[v]; if (!vis[pt[i].to]) { vis[pt[i].to]=true; q.push(pt[i].to);}}} }} int main() { int u,v,sum,temp,val,t,cn,nu,ans=1,k,b; ll minn,mi;scanf("%d",&cn);while(cn--){int flag=1; mi=inf; minn=0; k=0; q=1; scanf("%d %d",&n,&m); memset(head,-1,sizeof(head)); for (int i=1;i<=m;i++) { scanf("%d %d",&t,&nu); for (int j=0;j<nu;j++) { scanf("%d",&b); { add(b,n+i,t); add(n+i,b,t);}} } SPFA(1); SPFA2(n); for (int i=1;i<=n;i++) { if (dis[i]==inf||di[i]==inf) continue; minn=max(di[i],dis[i]); di[i]=dis[i]=minn; if (minn<mi) { mi=minn; } } printf("Case #%d: ",ans++); if (mi==inf) cout<<"Evil John"<<endl; else { printf("%lld\n",mi/2); for (int i=1;i<=n;i++) { if (mi==di[i]) { if (flag) { printf("%d",i); flag=0; } else printf(" %d",i); } } printf("\n"); } } return 0; }
阅读全文
0 0
- HDU 5521 Meeting
- HDU-5521 Meeting(Dijkstra)
- HDU 5521Meeting
- HDU 5521 Meeting
- hdu 5521 Meeting
- HDU 5521-Meeting(最短路)
- HDU 5521 Meeting 最短路
- HDU 5521 Meeting【最短路】
- [hdu 5521 Meeting] 拆边 + Dijkstra
- HDU 5521 Meeting(最短路)
- HDU 5521 Meeting(最短路)
- 【最短路】hdu 5521 Meeting
- Meeting HDU
- Meeting HDU
- HDU-5510 Bazinga、HDU-5521 Meeting
- HDU 5521 Meeting (最短路)
- hdu 5521 Meeting(最短路)
- hdu-5521-Meeting- 最短路-增点建图
- 《加密与解密》第三篇非明码比较分析,个人实战
- 【NOIP2016】换教室
- LinuxStudyNote(43)-图解Linux系统分区
- 刺杀
- some tips about python
- HDU 5521 Meeting
- 【Luogu3919】可持久化数组
- 组合数
- Swift开发指南:使用Swift与Cocoa和Objective-C(Swift 4)
- Bootstrap学习笔记——排版
- 关于atom插件下载难的问题
- uva 1151 Buy or Build
- div标签模拟textarea
- HDU 5510 Bazinga 多种姿势