hdu5521Meeting/2015acmicpc沈阳3号题(图论最短路)

来源:互联网 发布:网络热点事件2017 编辑:程序博客网 时间:2024/04/30 08:47
Meeting
Time Limit : 12000/6000ms (Java/Other)   Memory Limit : 262144/262144K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 0

Font: Times New Roman | Verdana | Georgia

Font Size:

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 m.2n105. 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

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

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.

题意不多费口舌了,直接AC思想吧

这题看上去是个最短路,但难点在于如何建图,如果直接暴力建图,相信这样多的边数没那么容易让你过,所以可以想想,新建点,将每个集合都新加一个点i+n,集合每个元素到这个点的权值为ti,这样,就有了n+m个点.(以第一个样例为例图示)

可以看出,这样的图,直接从1点跑一边最短路+heap,再从n点跑一边最短路+heap,得出结论。可以发现,最终的路程,是原路程的两倍(因为一个集合内任意2点之间的路线跑了2次)所以最后除以2即可。还要记得中间值可能会超int ,long long 存一下即可。(本人int存,蜜汁超时)

#include<cstdio>#include<cstdlib>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<string>#include<cstring>#include<cmath>#include<vector>#include<map>#include<set>#define eps 1e-8#define zero(x) (((x>0?(x):-(x))-eps)#define mem(a,b) memset(a,b,sizeof(a))#define memmax(a) memset(a,0x3f,sizeof(a))#define pfn printf("\n")#define ll __int64#define mod 1000000007#define sf(a) scanf("%d",&a)#define sf64(a) scanf("%I64d",&a)#define sf264(a,b) scanf("%I64d%I64d",&a,&b)#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)#define sf2(a,b) scanf("%d%d",&a,&b)#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)#define sff(a) scanf("%f",&a)#define sfs(a) scanf("%s",a)#define sfs2(a,b) scanf("%s%s",a,b)#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)#define sfd(a) scanf("%lf",&a)#define sfd2(a,b) scanf("%lf%lf",&a,&b)#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)#define sfc(a) scanf("%c",&a)#define debug printf("***\n")const double PI = acos(-1.0);const double e = exp(1.0);const int INF = 0x7fffffff;;template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }template<class T> inline T Min(T a, T b) { return a < b ? a : b; }template<class T> inline T Max(T a, T b) { return a > b ? a : b; }bool cmpbig(int a, int b){ return a>b; }bool cmpsmall(int a, int b){ return a<b; }using namespace std;#define MAX 2000010int head[MAX],cnt;int dis[2][MAX],n,m,vis[MAX];struct headtop{int v;ll d;headtop(int v=0,ll d=0):v(v),d(d) {}friend bool operator < (headtop a,headtop b){return a.d>b.d;}};struct aredge{int v;ll d;int next;aredge(int v=0,ll d=0,int next=0):v(v),d(d),next(next) {}}edge[MAX];void add_edge(int u,int v,int d){edge[cnt]=aredge(v,d,head[u]);head[u]=cnt++;}void init(){int i,j;sf2(n,m);mem(head,-1);cnt=0;for(i=0;i<m;i++){int num,d;sf2(d,num);for(j=0;j<num;j++){int v;sf(v);add_edge(i+1+n,v,d);add_edge(v,i+1+n,d);}}}void shortestpath(int s,int c){priority_queue<headtop>q;int i;mem(vis,0);for(i=1;i<=n+m;i++)dis[c][i]=INF;dis[c][s]=0;headtop now;now.v=s,now.d=0;q.push(now);while(!q.empty()){now=q.top(),q.pop();if(vis[now.v])continue;vis[now.v]=1;for(i=head[now.v];i+1;i=edge[i].next){if(dis[c][edge[i].v]>now.d+edge[i].d){dis[c][edge[i].v]=now.d+edge[i].d;q.push(headtop(edge[i].v,dis[c][edge[i].v]));}}}}void solve(int tot){shortestpath(1,0);shortestpath(n,1);int i;ll ans=INF;for(i=1;i<=n;i++)ans=min(ans,(ll)max(dis[0][i],dis[1][i]));if(ans==INF){printf("Case #%d: Evil John\n",tot);return ;}printf("Case #%d: %I64d\n",tot,ans/2);int flag=0;for(i=1;i<=n;i++)if(max(dis[0][i],dis[1][i])==ans)flag?printf(" %d",i):printf("%d",i),flag++;pfn;}int main(){int t,tot=1;sf(t);while(t--){init();solve(tot++);}    return 0;}

0 0
原创粉丝点击