hdu 5221 Meeting 建图+迪杰斯特拉

Meeting

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


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
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   5520  5519  5518  5517  5516 


重现赛上想出来的。这个题目的难点就是边数众多,正常思路根本没法建图。
建图:
m行,就是m个集合,每个集合看作一个点,对于集合A里面包含的每一个元素x,连一条边x->A,权值为t[A],连一条边A->x,权值为0。
所以共有m+n个点。

从点1和点n出发用两次(堆优化的)dijkstra。
自此从1到n的每个点都有从1和n出发到达它的最短时间。
取两个时间的最大值为在该点相遇的最短时间。再在所有点相遇时间中取最小值,即为最短时间。

#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk    make_pair
#define _f     first
#define _s     second
using namespace std;
//const int INF=    ;
typedef long long ll;
const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 1e6+1e5+20   ;
//const int maxm=    ;


int n,m;
int N;

vector<int >G[maxn];

struct Edge
{
    int from,to;
    ll dist;
    Edge(){}
    Edge(int from,int to,ll dist):from(from),to(to),dist(dist){}
};

vector<Edge>edges;
void add_edge(int from ,int to,ll dist)
{
    edges.push_back(Edge(from,to,dist));
    edges.push_back(Edge(to,from,0));
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}


void init()
{
    for(int i=1;i<=n+m;i++)
        G[i].clear();
    edges.clear();
}
ll d[2][maxn];


struct Node
{
    int index;
    ll dis;
    Node(){}
    Node(ll dis,ll index):dis(dis),index(index){}
    bool operator<(const Node y)const
    {
        return dis>y.dis;
    }
};

bool done[maxn];
void dijkstra(int now,int s)
{
    priority_queue<Node>q;
    for(int i=1;i<=N;i++)
    d[now][i]=inf;
    d[now][s]=0;
    memset(done,0,sizeof done);
    q.push(Node(0,s));
    while(!q.empty())
    {
        Node tmp=q.top();q.pop();
        int x=tmp.index;
        if(done[x])  continue;
        done[x]=1;
        for(int i=0;i<G[x].size();i++)
        {
            int ed=G[x][i];
            Edge &e=edges[ed];
            int y=e.to;
            if(done[y])  continue;
            if(d[now][y]>d[now][x]+e.dist)
            {
                d[now][y]=d[now][x]+e.dist;
                q.push(Node(d[now][y],y));
            }
        }
    }

}
vector<int >ve;
int main()
{
    int T,kase=0;
    scanf("%d",&T);

    while(T--)
    {
        scanf("%d%d",&n,&m);
        N=n+m;
        ll t;int k,x;
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&t);
            scanf("%d",&k);
            for(int j=1;j<=k;j++)
            {
              scanf("%d",&x);
              add_edge(  x,i+n,t );
            }
        }
        dijkstra(0,1);
        dijkstra(1,n);
        ve.clear();
        ll mini=1e15;
        for(int i=1;i<=n;i++)
        {
            ll tmp=max(d[0][i],d[1][i]);
            if(tmp>mini) continue;
            else if(tmp==mini)  ve.push_back(i);
            else
            {
                mini=tmp;
                ve.clear();
                ve.push_back(i);
            }
        }
        printf("Case #%d: ",++kase);
        if(mini==inf)  {puts("Evil John");continue;}
        printf("%lld\n",mini);
        for(int i=0;i<ve.size();i++)
        {
            if(i==0)  printf("%d",ve[i]);
            else  printf(" %d",ve[i]);
        }
        putchar('\n');


    }


    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值