HDU-5521 类似超级汇点起点的建图(新题)

Meeting

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.

题意:n个点,然后有m个集合,集合里面的点都是相互连通的,并且权值都是相等的,然后有2个人,我们定为A,B,这两人要去碰面,碰面所需要花费的时间是边上的权值;
A从1号点出发,B从n号点出发,两人碰面的最短时间是多少,如果存在一个合法解的话,就输出两人碰面的地方,如果有多个,那么就按照递增的顺序输出即可,如果没有的话,那就是输出 Evil John

分析:
首先,如果按照题意那样去建边的话,空间上是不允许的,边的数目太多;
那么则怎么去处理呢??
学过网络流的话,就知道超级汇点和超级起点的这个概念(不知道的话没有关系),那么处理的方法就是按照这个思路来的的;
大家看下面的这个图
在这里插入图片描述
假设上图中的黑点就是我们建立的中间汇点,那么这样处理之后,只需要建立4条边就好了,否则对于四个点的图来说,就需要建立6条边了;

通过上面这样的方法,空间和时间的复杂度都能够降下来;

ps:1.边的数目要开大一些;
2.答案要用long long

那么剩下的就是从1–n和从n–1跑两场最短路的模板就好了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<ctime>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=1e5+10;
const ll INF=0x3f3f3f3f3f3f3f3f;
struct node1{
    int now;ll dis;
    bool operator < (const node1 & a)const{
        return dis>a.dis;
    }
};
priority_queue<node1>p;
struct node{
    int to,next;ll val;
}num[N*30];
int tot,head[N<<2],vis[N<<2],m,n,ext;
ll dis1[N<<2],dis2[N<<2];
void add(int u,int v,ll val){
    num[tot].next=head[u];
    num[tot].to=v;
    num[tot].val=val;
    head[u]=tot++;
}
void di(int st,ll dis[]){
    for(int i=1;i<=ext;i++) dis[i]=INF,vis[i]=0;
    dis[st]=0;
    vis[st]=1;
    p.push((node1){st,0});
    while(!p.empty()){
        node1 u=p.top();
        p.pop();
        for(int i=head[u.now];i!=-1;i=num[i].next){
            if(vis[num[i].to]) continue;
            if(dis[num[i].to]>dis[u.now]+num[i].val){
                dis[num[i].to]=dis[u.now]+num[i].val;
                p.push((node1){num[i].to,dis[num[i].to]});
                vis[num[i].to]=1;
            }
        }
    }
//     for(int i=1;i<=n;i++)
//            cout<<dis[i]<<" ";
//        cout<<endl;
}
int pos[N];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    for(int o=1;o<=T;o++){
        memset(head,-1,sizeof head);
        printf("Case #%d: ",o);
        scanf("%d%d",&n,&m);
        ext=n;
        for(int i=1;i<=m;i++){ll val;int no;
         ext++;
            scanf("%lld%d",&val,&no);
            while(no--){int u;
                scanf("%d",&u);
                add(u,ext,val);
                add(ext,u,val);
            }
        }
        di(1,dis1);
        di(n,dis2);
        ll ans=INF;
        int cnt=0;
        for(int i=1;i<=n;i++){
            ll temp=max(dis1[i],dis2[i]);
            if(temp<ans){
                cnt=0;
                pos[cnt++]=i;
                ans=temp;
            }else if(temp==ans){
                pos[cnt++]=i;
            }
        }

        if(ans<INF){
            printf("%lld\n",ans/2);
            for(int i=0;i<cnt;i++)
                printf("%d%c",pos[i],i==cnt-1?'\n':' ');
        }else{
            printf("Evil John\n");
        }
    }
    return 0;
}


发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/101632509