HDU5521 Meeting (最短路)

Meeting

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


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  m 2n105 . 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.
 

Source

题意:有n个顶点,m个集合,其中一个人在顶点1,另一个人在顶点n,求两人相遇时花费的最小时间以及可能相遇的所有顶点

思路:如果直接按照顶点个数建边,则时间复杂度为O(n^2),但是题目给出的是一个集合,在一个集合中每个顶点之间的距离是相等的,这时候,对于一个集合来说,我们就再额外的增加一个顶点,把该集合中所有的顶点与该顶点相连,使得从集合中的顶点到该集合外新增的顶点的代价为w,而从该新增的顶点到集合中的顶点的花费为0,这样建图的话花费的时间为O(2*n),然后分别求从1顶点和n顶点到其他各个顶点的最短路即可。


SPFA实现:

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define ll long long
#define inf 1ll << 60
using namespace std;

const int maxn = 1e6 + 10;
struct Edge{
    int w,to,next;
}edge[maxn<<1];
int head[maxn<<1],n,m,t,cnt;
ll disx[maxn],disy[maxn],dis[maxn],vis[maxn];

void init(){
    memset(head,-1,sizeof(head));
    cnt = 0;
}

void add(int u, int v, int w){
    edge[cnt].w = w;
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt ++;
}

void spfa(int s, int nn){
    for(int i = 1; i <= nn; i ++) dis[i] = inf;
    memset(vis,0,sizeof(vis));
    dis[s] = 0;
    vis[s] = 1;
    queue<int > q;
    q.push(s);
    while(!q.empty()){
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(dis[v] > dis[u] + edge[i].w){
                dis[v] = dis[u] + edge[i].w;
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}
int main(){
    scanf("%d",&t);
    int cas = 0;
    while(t --){
        init();
        scanf("%d%d",&n,&m);
        int start = n + 1;
        for(int i = 1; i <= m; i ++){
            int w,s,v;
            scanf("%d%d",&w,&s);
            for(int i = 1; i <= s; i ++){
                scanf("%d",&v);
                add(v,start,w);
                add(start,v,0);
            }
            start ++;
        }
        spfa(1,n+m+1);
        if(dis[n] == inf){
            printf("Case #%d: Evil John\n",++cas);
            continue;
        }
        for(int i = 1; i <= n; i ++) disx[i] = dis[i];
        spfa(n,n+m+1);
        for(int i = 1; i <= n; i ++) disy[i] = dis[i];
        ll ans = inf;
        int flag=0;
        for(int i = 1; i <= n; i ++){
            vis[i] = max(disx[i],disy[i]);
            if(ans > vis[i]) ans = vis[i],flag = i;
        }
        printf("Case #%d: %d\n",++cas,ans);
        printf("%d",flag);
        for(int  i = 1; i <= n; i ++){
            if(vis[i] == ans && i != flag) printf(" %d",i);
        }
        printf("\n");
    }
    return 0;
}

dijkstra实现:

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define ll long long
#define inf 1ll << 60
using namespace std;

const int maxn = 1e6 + 10;
struct Edge{
    int w,next,to;
}edge[maxn << 1];
struct node{
    ll dis;
    int v;
    friend bool operator < (node A, node B){
        return A.dis > B.dis;
    }
};
ll dis[maxn],vis[maxn],disx[maxn],disy[maxn];
int head[maxn],n,m,t,cnt;

void add(int u, int v, int w){
    edge[cnt].w = w;
    edge[cnt].to = v;
    edge[cnt].next = head[u];
    head[u] = cnt ++;
}

void dijkstra(int s, int nn){
    priority_queue<node > q;
    for(int i = 1; i <= nn; i ++) dis[i] = inf;
    memset(vis,0,sizeof(vis));
    dis[s] = 0; vis[s] = 1;
    node p;
    p.dis = 0; p.v = s;
    q.push(p);
    while(!q.empty()){
        p = q.top(); q.pop();
        for(int i = head[p.v]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(!vis[v] && dis[v] > dis[p.v] + edge[i].w){
                dis[v] = dis[p.v] + edge[i].w;
                node no;
                no.dis = dis[v]; no.v = v;
                q.push(no);
            }
        }
    }
}

int main(){
    int cas = 0;
    scanf("%d",&t);
    while(t --){
        memset(head,-1,sizeof(head));
        cnt = 0;
        scanf("%d%d",&n,&m);
        int start = n+1;
        for(int i = 1; i <= m; i ++){
            int u,w,s;
            scanf("%d%d",&w,&s);
            for(int j = 1; j <= s; j ++){
                scanf("%d",&u);
                add(u,start,w);
                add(start,u,0);
            }
            start ++;
        }
        dijkstra(1,n+m+1);
        for(int i = 1; i <= n; i ++) disx[i] = dis[i];
        if(dis[n] == inf){
            printf("Case #%d: Evil John\n",++cas);
            continue;
        }
        dijkstra(n,n+m+1);
        for(int i = 1; i <= n; i ++) disy[i] = dis[i];
        int indx;
        ll ans = inf;
        for(int i = 1; i <= n; i ++){
            vis[i] = max(disx[i],disy[i]);
            if(ans > vis[i]){
                ans = vis[i]; indx = i;
            }
        }
        printf("Case #%d: %lld\n",++cas,ans);
        printf("%d",indx);
        for(int i = 1; i <= n; i ++){
            if(vis[i] == ans && i != indx) printf(" %d",i);
        }
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值