UVALive7250 Meeting 2015 ACM/ICPC 沈阳区域赛 M—Meeting

这篇博客介绍了如何解决一个关于路径规划的问题。Bessie和Elsie被篱笆分隔在不同的区块,他们需要找出最快能见面的地点和时间。题目给出了每个区块内的旅行时间和区块集合,通过建立虚拟原点和使用Dijkstra算法,计算出每个区块到起点和终点的最短时间,从而确定最优会面点。博客提供了详细的解题思路和C++代码实现。
摘要由CSDN通过智能技术生成

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.
思路:
对于每一个set建立一个虚拟原点,每个set中的点只与虚拟远点建立无向边,边权值为这个set的t/2。
这样就可以保证在建边较少的情况下使set内部两点之间的距离为t。

从1跑一边dijkstra,得到到达每个点的最小值。
从n跑一边dijkstra,得到到达每个点的最小值。

枚举给个点的dist取一遍最大值,将该值存储在mx中,表示如果在这个点见面,需要多少分钟。
假设在所有点中见面所需要的最小值res,那么所有 m x [ i ] = = r e s mx[i]==res mx[i]==res的点,都可以作为见面的点,打印出i即可。
代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<double,int>PDI;
const int N=1e7+10;
int e[N],ne[N],h[N],idx=0;
double w[N];
double dist1[N],dist2[N];
bool st[N];
int n,m;
int mx[N];
void add(int a,int b,double c)
{
    w[idx]=c;
    e[idx]=b;
    ne[idx]=h[a];
    h[a]=idx++;
}
void dijkstra(int u,double dist[])
{
    for(int i=1;i<N;i++)
    {
        dist[i]=0x3f3f3f3f;
    }
    memset(st,false,sizeof st);
    priority_queue<PDI, vector<PDI>, greater<PDI> >q;
    q.push({ 0,u });
    dist[u] = 0;
    while (!q.empty())
    {
        PDI t = q.top();
        q.pop();
        int pos = t.second;
        double dis = t.first;
        if (st[pos])
        {
            continue;
        }
        st[pos] = true;
        for (int i = h[pos]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dis + w[i])
            {
                dist[j] = dis + w[i];
                q.push({ dist[j],j });
            }
        }
    }
}
inline int read()
{
    int X=0; bool flag=1; char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
    while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
    if(flag) return X;
    return ~(X-1);
}

int main()
{
    int T;
    cin>>T;
    for(int p=1;p<=T;p++)
    {
        idx=0;
        memset(h,-1,sizeof h);
        n=read();
        m=read();        
        for(int i=1;i<=m;i++)
        {
            int t,s;
            t=read();
            s=read();
            double va=(t*1.0)/2;
            int e1=i+n;
            for(int j=0;j<s;j++)
            {
                int k;
                k=read();
                add(e1,k,va);
                add(k,e1,va);
            }
        }
        dijkstra(1,dist1);
        dijkstra(n,dist2);
        int res=0x3f3f3f3f;
        for(int i=1;i<=n;i++)
        {
            mx[i]=max(dist1[i],dist2[i]);
            res=min(res,mx[i]);
        }
        cout<<"Case #"<<p<<": ";
        if(res==0x3f3f3f3f)
        {
            cout<<"Evil John"<<endl;
            continue;
        }
        cout<<res<<endl;
        bool flag=true;
        for(int i=1;i<=n;i++)
        {
           	if(res==mx[i])
           	{
           		if(!flag)
           		{
           			cout<<' ';
				}
           		flag=false;
           		cout<<i;
        	}
        }
        cout<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值