HDU5521Meeting(最短路建图)

Meeting

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


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 (1im)  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 (1T6) , the number of test cases. Then  T  test cases
follow.

The first line of input contains  n  and  m 2n105 . The following  m  lines describe the sets  Ei (1im) . Each line will contain two integers  ti(1ti109) and  Si (Si>0)  firstly. Then  Si  integer follows which are the labels of blocks in  Ei . It is guaranteed that  mi=1Si106 .
 

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


题意:

给n个点,m个集合,每个集合里有si 个点,相同集合内部点的距离为fi,A在1号点,B在n号点,A B要见面,他们只能在节点见面不可以在路上见面,AB走单位距离话费1分钟,问最少话费几分钟可以见面,在最小话费的前提下有几个节点可以作为见面的点,按照字典序输出可以见面的地点,如果没有输出Evil John 。 

思路:
这道题目的难点在于没有办法给所有点两两之间建边,因为点的个数是100000,但是这个题给了集合这个东西,在每个集合里两点之间的距离是固定的,所以我们可以增加m个点,每个集合增加一个点,然后把所有点的出边指向新增的点,权值为w,然后再从新增的点指回来,权值为0,这样的话相当于建边的时间复杂度从n方降为2n,这样的话一切就都容易了。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<ll, int>
const int maxn = 2e5+5;
const int maxe = 2e6+5;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int head[maxn], cnt;
int ans[maxn];
ll dis[maxn][2];
bool vis[maxn];
struct Edge
{
    int v, nxt;
    ll w;
    Edge(){}
    Edge(int v, ll w, int nxt):v(v), w(w), nxt(nxt){}
}edge[maxe];
void addedge(int from, int to, ll w)
{
    edge[cnt] = Edge(to, w, head[from]);
    head[from] = cnt++;
}
void Dijkstra(int s, int k)
{
    memset(vis, false, sizeof(vis));
    dis[s][k] = 0;
    priority_queue<pii, vector<pii>, greater<pii> > q;
    q.push(pii(dis[s][k], s));
    while(!q.empty())
    {
        pii temp = q.top();
        q.pop();
        int u = temp.second;
        if(vis[u]) continue;
        vis[u] = true;
        for(int i = head[u]; i != -1; i = edge[i].nxt)
        {
            int to = edge[i].v;
            if(dis[to][k] > dis[u][k] + edge[i].w)
            {
                dis[to][k] = dis[u][k] + edge[i].w;
                q.push(pii(dis[to][k], to));
            }
        }
    }
}
int main()
{
    int t, n, m, s, u, v;

    scanf("%d", &t);
    for(int kase = 1; kase <= t; kase++)
    {
        cnt = 0;
        memset(head, -1, sizeof(head));
        scanf("%d%d", &n, &m);
        for(int k = 1; k <= m; k++)
        {
            ll w;
            scanf("%I64d%d", &w, &s);
            for(int i = 1; i <= s; i++)
            {
                scanf("%d", &u);
                addedge(u, n+k, w);
                addedge(n+k, u, 0);
            }
        }
        for(int i = 1; i <= n+m; i++)
            dis[i][0] = dis[i][1] = INF;
        Dijkstra(1, 0);
        Dijkstra(n, 1);
        int tol = 0;
        ll ma = INF;
        for(int i = 1; i <= n; i++)
        {
            ll ret = max(dis[i][0], dis[i][1]);
            if(ret < ma)
            {
                ma = ret;
                tol = 0;
                ans[tol++] = i;
            }
            else if(ret == ma && ret != INF)
                ans[tol++] = i;
        }
        if(tol == 0)
            printf("Case #%d: Evil John\n", kase);
        else
        {
            printf("Case #%d: %d\n", kase, ma);
            for(int i = 0; i < tol; i++)
                printf("%d%c", ans[i], i == tol-1 ? '\n' : ' ');
        }

    }
    return 0;
}









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值