hdu5521 Meeting最短路

本文深入探讨了信息技术领域的多个细分技术领域,包括前端开发、后端开发、移动开发、游戏开发等,提供了对不同技术领域的全面概述,旨在帮助读者了解信息技术产业的最新趋势和发展。

Meeting

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


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

题意:给出一些点和边构成一个图,John从1点出发,Elsie从n点出发,问他们最快需要多久相遇。
输入数据:
n m
有m行集合关系,每行有si个点,他们之间的距离都是ti。
思路:
求最短路问题,不过正常建边可能会多达1e12条边,再求最短路的话会超时。
一共有m个集合关系,第i个集合有Si个点,向每个集合增加2个点(in和out),将集合内所有的点连一条边到in(权值为0),out向每个点连一条边(权值为0),in再向out建一条边(权值为ti)。
这样就可以大大减少建边数量(3e6)。最后只要进行两次最短路,就可以找出最短时间了。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define ll long long int
const int maxH = 3e6+10;
#define inf 999999999
struct Edge{
    int to;
    ll dis;
    Edge(){};
    Edge(int to_, ll dis_){to = to_, dis = dis_;}
    bool operator < (const Edge& b)const{return dis>b.dis;}
}edge[maxH];
int head[maxH], tot, nxt[maxH];
ll A[maxH], B[maxH];
bool vis[maxH];
void add(int x, int y, ll dis){
    edge[tot] = Edge(y, dis);
    nxt[tot] = head[x];
    head[x] = tot++;
}
void dijkstra(int st, ll f[]){
    for(int i = 0;i <= maxH;i++) f[i] = inf;
    priority_queue<Edge>q;
    memset(vis, 0, sizeof vis);
    f[st] = 0;
    q.push(Edge(st, 0));
    while(!q.empty()){
        int x = q.top().to;
        q.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(int i = head[x];i != -1;i = nxt[i]){
            Edge e = edge[i];
            //printf("%I64d %I64d %I64d\n", f[e.to], f[x], e.dis);
            if(f[e.to] > f[x] + e.dis){
                f[e.to] = f[x] + e.dis;
                q.push(Edge(e.to, f[x] + e.dis));
            }
        }
    }
}
int main(){
    int T, n, m, t, s, k, e;
    scanf("%d", &T);
    k = 1;
    while(T--){
        tot = 1;
        scanf("%d %d", &n, &m);
        memset(head, -1, sizeof head);
        for(int i = 1;i <= m;i++){
            scanf("%d %d", &t, &s);
            int in = n + i;
            int out = n + m + i;
            add(in, out, t);
            while(s--){
                scanf("%d", &e);
                add(e, in, 0);
                add(out, e, 0);
            }
        }
        dijkstra(1, A);
        dijkstra(n, B);
        ll ans = inf;
        int ed;
        for(int i = 1;i <= n;i++){
            ll tmp = max(A[i], B[i]);
            //printf("%I64d %I64d\n", A[i], B[i]);
            if(tmp <= ans){
                ans = tmp;
                ed = i;
            }
        }
        printf("Case #%d: ", k++);
        if(ans == inf) printf("Evil John\n");
        else {
            printf("%I64d\n", ans);
            for(int i = 1;i < ed;i++){
                if(max(A[i], B[i]) == ans)
                    printf("%d ", i);
            }
            printf("%d\n", ed);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值