Meeting (完全图建图+最短路)

本文介绍了一个关于Bessie和Elsie在被分隔的农场区块中寻找最快会面地点的问题。每个区块属于特定集合,两头牛需要找出集合间的最短路径并确定会面时间及地点。题目要求通过输入数据计算最短会面时间,如果无法会面则输出'Evil John'。解决方案包括构建图并应用最短路径算法。

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

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 ∑m
i=1 Si ≤ 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.
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.
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

题目大概:

给你n个点,m个团,每个团的点都是完全图,每个团的边权一样,都是w。然后两个人从1 ,n 出发,到达各个点的最少时间是多少,最少时间的点全部输出,如果不通,输出Evil John。

思路:

主要是建图的时候,可以把一个团的外面抽象一个虚点,然后把每个点的边权都连向虚点,这样,团内的点的距离问题,和两个团重合的问题也都解决了。但是权值加了一倍,最后需要  / 2.

当建完图就是最短路的模板题了。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int MAXN=2000010;
struct qnode
{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c) {}
    bool operator <(const qnode &r)const
    {
        return c>r.c;
    }
};
struct Edge
{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
//点的编号从 1 开始
void Dijkstra(int n,int start)
{
    memset(vis,false,sizeof(vis));
    for(int i=1; i<=n; i++)dist[i]=INF;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[start]=0;
    que.push(qnode(start,0));
    qnode tmp;
    while(!que.empty())
    {
        tmp=que.top();
        que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=true;
        for(int i=0; i<E[u].size(); i++)
        {
            int v=E[tmp.v][i].v;
            int cost=E[u][i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
int a[MAXN],b[MAXN],c[MAXN];
int main()
{
    int t;
    int ans=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n+m;i++)E[i].clear();
        for(int i=1; i<=m; i++)
        {
            int w,m1;
            scanf("%d%d",&w,&m1);
            for(int j=1; j<=m1; j++)
            {
                int u;
                scanf("%d",&u);
                addedge(u,i+n,w);
                addedge(i+n,u,w);
            }
        }
        Dijkstra(n+m,1);
        for(int i=1;i<=n;i++)
        {
            a[i]=dist[i];
        }
        Dijkstra(n+m,n);
        for(int i=1;i<=n;i++)
        {
            b[i]=dist[i];
        }
        int min_1=INF;
        for(int i=1;i<=n;i++)
        {
            c[i]=max(a[i],b[i]);
            min_1=min(min_1,c[i]);
        }
        if(min_1==INF)
        {
            printf("Case #%d: Evil John\n",ans++);
            continue;
        }
        printf("Case #%d: %d\n",ans++,min_1/2);
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            if(c[i]==min_1)
            {
                printf("%s%d",flag==1?" ":"",i);
                flag=1;
            }
        }
        puts("");
    }
    return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值