HDU

来源:互联网 发布:赵氏孤儿知乎 编辑:程序博客网 时间:2024/05/18 12:02

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 nn blocks labelled from 11 to nn.
Bessie lives in the first block while Elsie lives in the nn-th one. They have a map of the farm
which shows that it takes they titi minutes to travel from a block in EiEi to another block
in EiEi where Ei (1≤i≤m)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.
Input
The first line contains an integer T (1≤T≤6)T (1≤T≤6), the number of test cases. Then TT test cases
follow.

The first line of input contains nn and mm. 2≤n≤1052≤n≤105. The following mm lines describe the sets Ei (1≤i≤m)Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)ti(1≤ti≤109) and Si (Si>0)Si (Si>0) firstly. Then SiSi integer follows which are the labels of blocks in EiEi. It is guaranteed that ∑mi=1Si≤106∑i=1mSi≤106.
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.

建立中间点跑最短路
答案/2就可以了

#include<bits/stdc++.h>using namespace std;typedef long long ll;template <class T> inline void in(T &x) {    T f = 1; char c; while ((c = getchar()) < '0' || c > '9') if (c == '-') f = -1;    x = c - '0';    while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; x *= f;}ll daon[1100005],daon2[1100005],dan[110000];int n,m,T,u=0,q,w,e;bool bj[1100005];vector<int>tu[1100005],fei[1100005];struct p{    int d,c;    bool operator < (const p&a)const    {        return c>a.c;    }};ll inf=0x3f3f3f3f3f3f3f3f;priority_queue<p>qq;main(){    in(T);    while(T--)    {        in(n),in(m);        memset(daon,0x3f,sizeof(daon));        memset(bj,0,sizeof(bj));        memset(daon2,0x3f,sizeof(daon2));        memset(dan,0,sizeof(dan));        for(int a=1;a<=n+m;a++)tu[a].clear(),fei[a].clear();        for(int a=1;a<=m;a++)        {            in(q),in(w);            for(int b=1;b<=w;b++)            {                in(e);                tu[n+a].push_back(e);                tu[e].push_back(n+a);                fei[n+a].push_back(q);                fei[e].push_back(q);            }        }        daon[1]=0;        qq.push({1,0});        while(!qq.empty())        {            p zs=qq.top();            qq.pop();            if(bj[zs.d])continue;            bj[zs.d]=1;            for(int a=0;a<tu[zs.d].size();a++)            {                if(bj[tu[zs.d][a]])continue;                if(daon[tu[zs.d][a]]>daon[zs.d]+fei[zs.d][a])                {                    daon[tu[zs.d][a]]=daon[zs.d]+fei[zs.d][a];                    qq.push({tu[zs.d][a],daon[tu[zs.d][a]]});                }            }        }        memset(bj,0,sizeof(bj));        /*for(int a=1;a<=n;a++)        {            cout<<daon[a]<<" ";        }//      return 0;cout<<endl;*/        daon2[n]=0;        qq.push({n,0});        while(!qq.empty())        {            p zs=qq.top();            qq.pop();            if(bj[zs.d])continue;            bj[zs.d]=1;            for(int a=0;a<tu[zs.d].size();a++)            {                if(bj[tu[zs.d][a]])continue;                if(daon2[tu[zs.d][a]]>daon2[zs.d]+fei[zs.d][a])                {                    daon2[tu[zs.d][a]]=daon2[zs.d]+fei[zs.d][a];                    qq.push({tu[zs.d][a],daon2[tu[zs.d][a]]});                }            }        }/*      for(int a=1;a<=n;a++)        {            cout<<daon2[a]<<" ";        }        return 0;*/      ll zdl=inf;        for(int a=1;a<=n;a++)        {            if(daon[a]==inf||daon2[a]==inf)continue;            zdl=min(zdl,max(daon[a],daon2[a]));        }        for(int a=1;a<=n;a++)        {            if(daon[a]==inf||daon2[a]==inf)continue;            if(zdl!=max(daon[a],daon2[a]))continue;            dan[++dan[0]]=a;        }        if(zdl==inf)        {            printf("Case #%d: Evil John\n",++u);            continue;        }        printf("Case #%d: %lld\n",++u,zdl/2);        int jc=0;        for(int a=1;a<=dan[0];a++)        {            if(jc)            {                printf(" %d",dan[a]);                continue;            }            jc=1;            printf("%d",dan[a]);        }        printf("\n");    }}
原创粉丝点击