Meeting+最短路

在被Farmer John的栅栏隔开的农场中,Bessie和Elsie试图找到最快的方式在指定的区块内会面。通过分析不同区块间的旅行时间,本算法提供了一个解决方案,确定了最佳会面地点和所需时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://blog.youkuaiyun.com/lwt36/article/details/49535209

 

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.

        
 集合缩点,添加一个M个源点,源点→点 权为w,点→源点 权为0
1开始跑一遍最短路,n开始跑一遍最短路,枚举相遇的点就可以了
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 1e6 + 10;

typedef long long LL;

int n, m;

int head[M + N], cnt;
struct Edge {
    int v, nxt, c;
} edges[(M + N) << 1];

bool add_edge(int u, int v, int c) {
    edges[cnt] = (Edge) {v, head[u], c};
    head[u] = cnt++;
}

LL dp[2][M + N];
bool done[M + N];

typedef pair<LL, int> P;
void dijkstra(int s, int k) {
    priority_queue<P, vector<P>, greater<P> > q;
    memset(dp[k], 0x3f, sizeof dp[k]);
    memset(done, false, sizeof done);
    dp[k][s] = 0; q.push(P(0, s));
    while(q.size()) {
        int u = q.top().second; q.pop();
        done[u] = true;
        for(int i = head[u]; ~i; i = edges[i].nxt) {
            int v = edges[i].v, c = edges[i].c;
            if(!done[v] && dp[k][v] > dp[k][u] + c) {
                dp[k][v] = dp[k][u] + c;
                q.push(P(dp[k][v], v));
            }
        }
    }
}

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    int t; scanf("%d", &t);
    int kase = 0;
    while(t--) {
        scanf("%d%d", &n, &m);
        cnt = 0; memset(head, -1, sizeof head);
        for(int i = 1; i <= m; ++i) {
            int w, k; scanf("%d%d", &w, &k);
            for(int j = 1; j <= k; ++j) {
                int x; scanf("%d", &x);
                add_edge(n + i, x, w);
                add_edge(x, n + i, 0);
            }
        }
        dijkstra(1, 0);
        dijkstra(n, 1);
        LL minv = 1e18;
        for(int i = 1; i <= n; ++i)
            minv = min(minv, max(dp[0][i], dp[1][i]));
        vector<int> ans;
        for(int i = 1; i <= n; ++i)
            if(minv == max(dp[0][i], dp[1][i])) ans.push_back(i);

        if(minv == 1e18) printf("Case #%d: Evil John\n", ++kase);
        else {
            printf("Case #%d: %I64d\n", ++kase, minv);
            for(int i = 0; i < ans.size(); ++i)
                printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值