HDU 5521 Meeting (最短路SPFA+建立虚点)

Meeting

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

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 (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), the number of test cases. Then T test cases
follow.

The first line of input contains n 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.

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.

思路:不可能两两点相互之间建立边,这样边数太多肯定爆,但考虑这些点是在一个集合里的,这样对于每一个集合建立一个集合点,所有集合内的点指向这个点,建立两个有向边,一个为0,另一个为ti,这样就可以实现集合内点的连接了,然后跑一个最短路。

代码:

#include
using namespace std;
const int maxn=2e6+10;

struct ed{
    int  to;
    int next;
    int val;
}e[maxn];
int head[maxn];
int tot;
int n,m;
long long d1[maxn],d2[maxn];
long long d[maxn];
int vis[maxn];
void add(int f,int to,int val){
    e[tot].to=to;
    e[tot].val=val;
    e[tot].next=head[f];
    head[f]=tot++;
}

void SPFA(int s,int nn){
    for(int i=0;i<=nn;i++) d[i]=0x3f3f3f3f,vis[i]=0;
    queueq;
    while(!q.empty()) q.pop();
    q.push(s);
    vis[s]=1;
    d[s]=0;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].to;
            int w=e[i].val;
            if(d[v]>d[u]+w){
                d[v]=d[u]+w;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);
    int cas=1;
    while(t--){
            tot=0;
            memset(head,-1,sizeof(head));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++){
                int t1,t2;
                scanf("%d%d",&t1,&t2);
                for(int j=1;j<=t2;j++){
                    int tem;
                    scanf("%d",&tem);
                    add(tem,n+i,t1);
                    add(n+i,tem,0);
                }
            }

            SPFA(1,n+m+1);
            for(int i=1;i<=n;i++) d1[i]=d[i];
            SPFA(n,n+m+1);
            for(int i=1;i<=n;i++) d2[i]=d[i];

            int mi=0x3f3f3f3f;
            int f=0;
            for(int i=1;i<=n;i++){
                    long long tem=max(d1[i],d2[i]);
//                    printf("--%lld\n",tem);
                    if(mi>tem) f=i,mi=tem;
            }
            printf("Case #%d: ",cas++);
            if(mi==0x3f3f3f3f) printf("Evil John\n");
            else {
                printf("%lld\n",mi);
                printf("%d",f);
                for(int i=1;i<=n;i++) if(max(d1[i],d2[i])==mi&&i!=f) printf(" %d",i);
                printf("\n");
            }
    }
}

 

你可能感兴趣的:(SPFA)