HDU 5521 Meeting(最短路)

Meeting(传送门)

Time Limit : 12000/6000ms (Java/Other) Memory Limit : 262144/262144K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 8

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.
[/hint]

Source

2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

题意

给出 n 个顶点和m个点集,每一个点集中的任意两点之间的转移的时间花费是一样的,其中一个人在 1 号位置,一个人在n号位置,他们分别从这两个位置出发,请求出相遇的时间最短的位置。

解题思路

注意以下几点

  1. 相遇满足条件的位置可能存在多种,都需要输出
  2. 两个人都可以在某一位置进行等待(这是一个坑点)

然后由于每一个点集重元素的个数比较大,我们就需要增加两个点,在他们之间,即一个点集增加一个点 a ,让所有的点都连接他,代价为0,然后又增加点 b ,然后将a b 连接,代价为这个点集的时间t,然后连接 b 和这个点集的元素,代价为0,然后从 1 求一次最短路,然后从n求一次最短路,接着循环检测最短的时间就可以了

代码

/*除去冗长的头文件*/


const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 2e6 + 5;
const int INF = 0x3f3f3f3f;



int T, tot, n, m, t, s;
int Head[MAXN];
int V[MAXN];
LL d1[MAXN], d2[MAXN];
struct Edge{
    int to, nxt, cost;
}E[MAXN << 1];

void init_edge(){
    mem(Head, -1);
    tot = 0;
}

void add_edge(int u, int v, int cost){
    E[tot].to = v;
    E[tot].cost = cost;
    E[tot].nxt = Head[u];
    Head[u] = tot ++;
}

void BFS(int s, LL * d, int len){
    priority_queue<PLI, vector<PLI>, greater<PLI> >que;
    while(!que.empty()) que.pop();
    for(int i = 0;i <= len;i ++){
        d[i] = 1e19;
    }
    d[s] = 0;
    que.push(PLI(0, s));

    while(!que.empty()){
        PLI p = que.top();
        que.pop();
        int u = p.second;
        if(d[u] < p.first) continue;
        for(int v = Head[u]; ~v; v = E[v].nxt){
            Edge &e = E[v];
            if(d[e.to] > d[u] + e.cost){
                d[e.to] = d[u] + e.cost;
                que.push(PLI(d[e.to], e.to));
            }
        }
    }
}


int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();
    scanf("%d", &T);
    int cas = 1;
    while(T --){
        int e;
        init_edge();
        scanf("%d%d", &n, &m);
        int xv = n + 1;
        for(int i = 0;i < m;i ++){
            scanf("%d%d", &t, &s);
            for(int j = 0;j < s;j ++){
                scanf("%d", &V[j]);
            }
            for(int j = 0;j < s;j ++){
                add_edge(V[j], xv, 0);
            }
            add_edge(xv, xv + 1, t);
            for(int j = 0;j < s;j ++){
                add_edge(xv + 1, V[j], 0);
            }
            xv += 2;
        }
        BFS(1, d1, xv);
        BFS(n, d2, xv);
        LL ans = 1e19;
        printf("Case #%d: ", cas ++);
        for(int i = 1;i <= n;i ++){
            ans = min(ans, max(d1[i], d2[i]));
        }
        if(ans == (LL)1e19){
            printf("Evil John\n");
            continue;
        }
        printf("%I64d\n", ans);
        bool flag = false;
        for(int i = 1;i <= n;i ++){
            if(ans == max(d1[i], d2[i])){
                if(flag) printf(" ");
                printf("%d", i);
                flag = true;
            }
        }
        printf("\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值