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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章