hdu5521 ICPC2015沈阳现场赛(最短路)

                                   Meeting

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

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.
题意:给n个点,m个集合,每个集合里有si 个点,相同集合内部点的距离为fi,A在1号点,B在n号点,A B要见面,他们只能在节点见面不可以在路上见面,AB走单位距离话费1分钟,问最少话费几分钟可以见面,在最小话费的前提下有几个节点可以作为见面的点,按照字典序输出可以见面的地点,如果没有输出Evil John 。
这道题目的难点在于没有办法给所有点两两之间建边,因为点的个数是100000,但是这个题给了集合这个东西,在每个集合里两点之间的距离是固定的,所以我们可以增加m个点,每个集合增加一个点,然后把所有点的出边指向新增的点,权值为w,然后再从新增的点指回来,权值为0,这样的话相当于建边的时间复杂度从n方降为2n,这样的话一切就都容易了。我在一个地方错了很久,就是你新增加了这么多点,在预处理的时候和传点的数量的值的时候,传的一定是n+m+1,而不是n+1,这点需要注意

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const long long INF=pow(10,18);
const int maxm=2111110;
const int maxn=1111110;
struct sa
{
    int num;
    long long ww;
}ans[maxn];
struct EdgeNode
{
    int to;
    int w;
    int next;
};
EdgeNode edges[maxm];
int head[maxn],edge;
bool vis[maxn];
int a[maxn];
int outque[maxn];
queue<int>que;
long long dis[maxn];
long long disx[maxn],disy[maxn];
int T,n,m,t,s;
long long cmp(const sa x,const sa y)
{
    return x.wwvoid addedge(int u,int v,int c)
{
    edges[edge].w=c;
    edges[edge].to=v;
    edges[edge].next=head[u];
    head[u]=edge++;
}
void init()
{
    memset(head,-1,sizeof(head));
    edge=0;
}
void spfa(int s,int n)
{
    int u;
    for(int i=0; i<=m+n+5; i++)
        dis[i]=INF;
    memset(vis,0,sizeof(vis));
    while(!que.empty())
        que.pop();
    que.push(s);
    vis[s]=true;
    dis[s]=0;
//    memset(cnt,0,sizeof(cnt));
//    cnt[s]=1;
    while(!que.empty())
    {
        u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=head[u]; i!=-1; i=edges[i].next)
        {
            int v=edges[i].to;
            int w=edges[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
}
int top;

int main()
{
    int cases=1;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d",&n,&m);
        int start=n+1;
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&t,&s);
            for(int j=1; j<=s; j++)
            {
                scanf("%d",&a[j]);
                addedge(a[j],start,t);
                addedge(start,a[j],0);
            }
            start++;
        }
        spfa(1,n+m+1);
        for(int i=1; i<=n; i++)
        {
            disx[i]=dis[i];
        }
        spfa(n,n+m+1);
        for(int i=1; i<=n; i++)
        {
            disy[i]=dis[i];
        }
       printf("Case #%d: ",cases++);
        if(disx[n]==INF)
        {
          printf("Evil John\n");
            continue;
        }
        long long sum=INF;
        int flag=0;
         for(int i=1; i<=n; i++)
         {
             ans[i].ww=max(disx[i],disy[i]);
             if(sum>ans[i].ww)
             {
                 sum=ans[i].ww;
                 flag=i;
             }
             ans[i].num=i;
         }
         printf("%I64d\n",sum);
         printf("%d",flag);
         for(int i=1;i<=n;i++)
         {
             if(ans[i].ww==sum&&i!=flag)
                printf(" %d",i);
         }
         printf("\n");
    }
    return 0;
}

你可能感兴趣的:(图,最短路径,图论,最短路)