HDU 5521 2015ACM-ICPC沈阳赛区现场赛M题

Meeting

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

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 (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.

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

题意
就是说有n个点和m个块,每块包含si和ti,表示有si个点在第i块中,同块中的点距离为ti,不同块的点无法直接到达,问哪些块k可以作为见面的地方使得max(dist[1][k],dist[n][k])最小,输出满足的块编号

思路
这道题就是明显的最短路,对每一块建一个源点和汇点,源点指向块内所有城市,权值为0,块内所有城市指向汇点,权值为ti,Dij分别对1和n跑一遍,枚举每个点更新答案就行

#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int N = 1e5 + 2e6 + 10, M = 3e6 + 10;
int id;
int first[N], w[M], c[M], nxt[M];
LL f[N], F[N];
bool e[N];
struct node {
    int x;
    LL v;
    node() {
    }
    node(int x_, LL v_) {
        x = x_;
        v = v_;
    }
    bool operator <(const node& b) const {
        return v > b.v;
    }
};
void ins(int x, int y, int z) {
    id++;
    w[id] = y;
    c[id] = z;
    nxt[id] = first[x];
    first[x] = id;
}
priority_queue q;
void inq(int x, LL dis) {
    if (dis >= f[x])
        return;
    f[x] = dis;
    q.push(node(x, dis));
}
void dijkstra(int st) {
    memset(f, 63, sizeof(f));
    memset(e, 0, sizeof(e));
    inq(st, 0);
    q.push(node(st, 0));
    while (!q.empty()) {
        int x = q.top().x;
        q.pop();
        if (e[x])
            continue;
        e[x] = 1;
        for (int z = first[x]; z; z = nxt[z])
            inq(w[z], f[x] + c[z]);
    }
}
int main() {
    int T;
    int cas = 1;
    scanf("%d", &T);
    while (T--) {
        memset(first, 0, sizeof(first));
        id = 1;
        int n, m;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= m; i++) {
            int dis, g, x;
            scanf("%d%d", &dis, &g);
            int in = n + i;
            int out = n + m + i;
            ins(in, out, dis);
            while (g--) {
                scanf("%d", &x);
                ins(x, in, 0);
                ins(out, x, 0);
            }
        }
        dijkstra(1);
        memcpy(F, f, sizeof(F));
        dijkstra(n);
        LL ans = 1e18;
        int ed;
        for (int i = 1; i <= n; i++) {
            F[i] = max(F[i], f[i]);
            if (F[i] <= ans) {
                ans = F[i];
                ed = i;
            }
        }
        if (ans == 1e18)
            printf("Case #%d: Evil John\n", cas++);
        else {
            printf("Case #%d: %lld\n", cas++, ans);
            for (int i = 1; i < ed; i++)
                if (F[i] == ans)
                    printf("%d ", i);
            printf("%d\n", ed);
        }
    }
    return 0;
}

你可能感兴趣的:(图论,区域赛,最短路)