HDU5521Meeting(最短路建图)

来源:互联网 发布:邓肯季后赛数据 编辑:程序博客网 时间:2024/05/21 08:54

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 m2n105. 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
25 41 3 1 2 32 2 3 410 2 1 53 3 3 4 53 11 2 1 2
 

Sample Output
Case #1: 33 4Case #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
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)


题意:

给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;}