hdu5521 Meeting

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


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.
 
这题是一道最短路的题目,用普通的求法把边都保存下来会发现爆内存,所以考虑在每一个集合设置两个点作为入口和出口,该集合内的所有点与入口的距离是0,与出口的距离是0,入口与出口的距离是t,那么就可以大大减少边了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 99999999
#define maxn 1000050
int dist1[2*maxn+10],vis[2*maxn+10],dist2[2*maxn+10],first[2*maxn+10];
int n,m;
struct edge{
    int len,next,to;
}e[4*maxn];



struct node{
    int idx,dis;
}a,temp;

bool operator<(node a,node b){
    return a.dis<b.dis;
}
multiset<node>myset;
multiset<node>::iterator it;
void dijkstra1(int st)
{
    int i,j,v,u,mindis;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=2*maxn;i++)dist1[i]=inf;
    myset.clear();
    dist1[st]=0;vis[st]=1;
    a.dis=0;a.idx=st;
    myset.insert(a);
    while(!myset.empty()){
        it=myset.begin();
        a=*it;
        myset.erase(it);
        vis[a.idx]=1;
        for(i=first[a.idx ];i!=-1;i=e[i].next){
            v=e[i].to;
            if(!vis[v] && dist1[v]>a.dis+e[i].len){
                dist1[v]=a.dis+e[i].len;
                temp.dis=dist1[v];
                temp.idx=v;
                myset.insert(temp);
            }
        }
    }
}

void dijkstra2(int st)
{
    int i,j,v,u,mindis;
    memset(vis,0,sizeof(vis));
    for(i=1;i<=2*maxn;i++)dist2[i]=inf;
    myset.clear();
    dist2[st]=0;vis[st]=1;
    a.dis=0;a.idx=st;
    myset.insert(a);
    while(!myset.empty()){
        it=myset.begin();
        a=*it;
        myset.erase(it);
        vis[a.idx]=1;
        for(i=first[a.idx ];i!=-1;i=e[i].next){
            v=e[i].to;
            if(!vis[v] && dist2[v]>a.dis+e[i].len){
                dist2[v]=a.dis+e[i].len;
                temp.dis=dist2[v];
                temp.idx=v;
                myset.insert(temp);
            }
        }
    }
}

int main()
{
    int i,j,T,tot,cas=0,n1,t1,num,u,v,idx1,idx2,flag;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        tot=0;
        n1=maxn;
        memset(first,-1,sizeof(first));
        for(i=1;i<=m;i++){
            scanf("%d%d",&t1,&num);
            n1++;idx1=n1;
            n1++;idx2=n1;
            u=idx1;v=idx2;
            tot++;
            e[tot].next=first[u];e[tot].len=t1;e[tot].to=v;
            first[u]=tot;

            for(j=1;j<=num;j++){
                scanf("%d",&v);
                u=idx1;
                tot++;
                e[tot].next=first[v];e[tot].len=0;e[tot].to=u;
                first[v]=tot;

                u=idx2;
                tot++;
                e[tot].next=first[u];e[tot].len=0;e[tot].to=v;
                first[u]=tot;

            }

        }
        dijkstra1(1);
        dijkstra2(n);
        int mindis=inf;
        for(i=1;i<=n;i++){
            if(max(dist1[i],dist2[i])<mindis){
                mindis=max(dist1[i],dist2[i]);
            }
        }
        if(mindis==inf){
            cas++;
            printf("Case #%d: Evil John
",cas);
            continue;
        }
        cas++;
        printf("Case #%d: %d
",cas,mindis);
        flag=1;
        for(i=1;i<=n;i++){
            if(max(dist1[i],dist2[i])==mindis){
                if(flag){
                    printf("%d",i);
                    flag=0;
                }
                else printf(" %d",i);
            }
        }
        printf("
");
    }
    return 0;
}


原文地址:https://www.cnblogs.com/herumw/p/9464617.html