MEETING(完全图建图 最短路)

题目描述
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. Bessie lives in the 1st 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 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.

输入
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 Ei (1 ≤ Ei ≤ 109 ). Each line will contain two integers ti and Si firstly. Then Si integer follows and the blocks numbered of these integers forms the set Ei . It is guaranteed that

输出
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.

样例输入
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

样例输出
Case #1: 3
3 4
Case #2: Evil John

提示
In the first case, it will take Bessie 1 minutes to travel to the 3rd block, and it will take Elsie 3 minutes to travel to the 3rd block. It will take Bessie 3 minutes to travel to the 4th block, and it will take Elsie 3 minutes to travel to the 4th block.
In the second case, it is impossible for them to meet.

思路
这题难点在于对于一个完全图建图,如果用for循环来建图必然会T,因此我们虚构一个点,将每段点全部连接在该点上,这样建图最后得到的路径长度会加倍,所以最后得到答案时/2即可,最短路用dijkstra跑即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N=2000010;
const int INF=0x3f3f3f3f;
typedef pair<int,int> P;

struct node
{
    int v,w;
    node(int _v=0,int _w=0):v(_v),w(_w) {}
};

struct qnode
{
    int v,c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const qnode &r) const
    {
        return c>r.c;
    }
};

vector<node>E[N];

inline void addedge(int u,int v,int w)
{
    E[u].push_back(node(v,w));
    E[v].push_back(node(u,w));
}

bool vis[N];
int dist[N],tdis[N];
int n,m;

void dijkstra(int n,int s)
{
    for(int i=1;i<=n;i++)
    {
        dist[i]=INF;
        vis[i]=false;
    }
    priority_queue<qnode>que;
    que.push(qnode(s,0));
    dist[s]=0;
    while(!que.empty())
    {
        qnode 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[u][i].v;
            int cost=E[u][i].w;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    for(int id=1;id<=T;id++)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n+m;i++) E[i].clear();
        for(int i=1;i<=m;i++)
        {
            int t,s;
            scanf("%d%d",&t,&s);
            while(s--)
            {
                int p;
                scanf("%d",&p);
                addedge(p,n+i,t);
            }
        }
        dijkstra(n+m,1);
        for(int i=1;i<=n;i++) tdis[i]=dist[i];
        dijkstra(n+m,n);
        int mid=INF;
        for(int i=1;i<=n;i++)
        {
            dist[i]=max(dist[i],tdis[i]);
            mid=min(mid,dist[i]);
        }
        if(mid==INF)
        {
            printf("Case #%d: Evil John\n",id);
            continue;
        }
        printf("Case #%d: %d\n",id,mid/2);
        bool flag=false;
        for(int i=1;i<=n;i++)
        {
            if(dist[i]==mid)
            {
                if(flag) printf(" ");
                printf("%d",i);
                flag=true;
            }
        }
        puts("");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值