Meeting(未建边的最短路)

来源:互联网 发布:javascript 资源 网盘 编辑:程序博客网 时间:2024/05/01 21:36

第一次接触这种题目,a了一天,终于a过了,赶紧上课去

http://acm.hdu.edu.cn/showproblem.php?pid=5521

Meeting

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


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.
 
熟悉的迪杰斯特拉代码
#include <cstdio>#include <iostream>#include <queue>#include <vector>#include <cstring>using namespace std;const int N=100010;const long long maxn=1LL<<60;int n,m,s,t[N],a;vector<vector<int> > G,se;long long dis1[N],dis2[N];bool used1[N],used2[N];struct node{    int v;    long long w;    node(int vv,long long ww):v(vv),w(ww){}    bool operator <(const node &a)const {        return w>a.w;    }};void dijst(long long *dis,int s){    priority_queue<node> q;    memset(used1,0,sizeof(used1));    memset(used2,0,sizeof(used2));    for(int i=1;i<=n;i++)        dis[i]=maxn;    q.push(node(s,0));    while(!q.empty()){        node p=q.top();        q.pop();        int u=p.v;        if(used1[u])continue;        used1[u]=true;        dis[u]=p.w;        for(int i=0;i<G[u].size();i++){            int x=G[u][i];            if(used2[x])continue;            used2[x]=true;            int w=t[x];            for(int j=0;j<se[x].size();j++){                int v=se[x][j];                if(used1[v]||dis[v]<dis[u]+w)continue;                dis[v]=dis[u]+w;                q.push(node(v,dis[v]));            }        }    }}int main(){    int T;    scanf("%d",&T);    for(int cases=1;cases<=T;cases++){        scanf("%d%d",&n,&m);        G.clear();        se.clear();        G.resize(n+10);        se.resize(m+10);        for(int i=1;i<=m;i++){            scanf("%d%d",&t[i],&s);            for(int j=1;j<=s;j++){                scanf("%d",&a);                G[a].push_back(i);                se[i].push_back(a);            }        }        dijst(dis1,1);        dijst(dis2,n);        long long ans=maxn;        for(int i=1;i<=n;i++){            dis1[i]=max(dis1[i],dis2[i]);            ans=min(dis1[i],ans);        }        if(ans==maxn){            printf("Case #%d: Evil John\n",cases);            continue;        }        int cnt=0;        for(int i=1;i<=n;i++)            if(dis1[i]==ans)            dis2[cnt++]=i;        printf("Case #%d: %I64d\n",cases,ans);        printf("%I64d",dis2[0]);        for(int i=1;i<cnt;i++)            printf(" %I64d",dis2[i]);        printf("\n");    }    return 0;}



0 0
原创粉丝点击