zoukankan      html  css  js  c++  java
  • HDU 5521:Meeting(最短路)

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

    Meeting

    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
     
    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个集合,每个集合给出一个dis和s个点,这个集合里面的点去其他点的花费是dis,然后求从1到n和从n到1同时走到相同的点的最短距离。
    思路:由于给出的点的关系是集合,而且数据范围不允许我们两两建边,看了一下别人的结题报告,发现一个比较巧妙的方法:我们可以在这些点之外再弄出m个点来,每个集合的点和n之外的一个点存在边,这样的话每个集合的建边就是2*s而已,这样的建边让距离乘以2了,我们可以先保留这个距离,到输出答案时候再除以2.
     
      1 #include <cstdio>
      2 #include <algorithm>
      3 #include <queue>
      4 #include <cstring>
      5 #include <iostream>
      6 using namespace std;
      7 typedef long long LL;
      8 #define N 1000005
      9 #define M 2000005
     10 const LL inf = 1e17;
     11 /*
     12 最短路
     13 */
     14 struct node
     15 {
     16     int v, nxt;
     17     LL w;
     18 }edge[M];
     19 
     20 struct nd
     21 {
     22     int p;
     23     LL d;
     24     nd(){}
     25     nd(LL _d, int _to):d(_d), p(_to) {}
     26     bool operator < (const nd &a) const {
     27         return d > a.d;
     28     }
     29 };
     30 
     31 int tot, n, m, nn, used[N], head[N];
     32 LL da[N], db[N];
     33 
     34 void add(int u, int v, LL w)
     35 {
     36     edge[tot].v = v;
     37     edge[tot].w = w;
     38     edge[tot].nxt = head[u];
     39     head[u] = tot++;
     40 }
     41 
     42 void Dijkstra(int s, LL d[])
     43 {
     44     priority_queue<nd> que;
     45     while(!que.empty()) que.pop();
     46     for(int i = 0 ; i <= nn; i++) d[i] = inf;
     47     memset(used, 0, sizeof(used));
     48     d[s] = 0;
     49     que.push(nd(d[s], s));
     50     while(!que.empty()) {
     51         nd top = que.top(); que.pop();
     52         int u = top.p;
     53         if(used[u]) continue;
     54         used[u] = 1;
     55         for(int k = head[u]; ~k; k = edge[k].nxt) {
     56             int v = edge[k].v;
     57             int w = edge[k].w;
     58             if(d[u]+w < d[v]) {
     59                 d[v] = d[u] + w;
     60                 que.push(nd(d[v], v));
     61             }
     62         }
     63     }
     64 }
     65 
     66 int main()
     67 {
     68     int t;
     69     scanf("%d", &t);
     70     for(int cas = 1; cas <= t; cas++) {
     71         scanf("%d%d", &n, &m);
     72         memset(head, -1, sizeof(head));
     73         tot = 0;
     74         nn = n;
     75         for(int i = 0; i < m; i++) {
     76             LL dis;
     77             int num, a;
     78             //就是这里了,在外面建一个点,然后集合里面所有点都和它相连
     79             nn++;
     80             scanf("%I64d%d", &dis, &num);
     81             for(int j = 0; j < num; j++) {
     82                 scanf("%d", &a);
     83                 add(a, nn, dis);
     84                 add(nn, a, dis);
     85             }
     86         }
     87 
     88         Dijkstra(1, da);
     89         Dijkstra(n, db);
     90         LL ans = inf;
     91 
     92 
     93         for(int i = 1; i <= n; i++)
     94             ans = min(ans, max(da[i], db[i]));
     95 
     96         printf("Case #%d: ", cas);
     97         if(ans==inf) printf("Evil John
    ");
     98         else{
     99             //因为求出的距离是两倍,所以要除以二
    100              printf("%I64d
    ", ans/2);
    101              bool f = 0;
    102              for(int i = 1; i <= n; i++) {
    103                 if(max(da[i], db[i]) == ans){
    104                     if(f) printf(" ");
    105                     f = 1;
    106                     printf("%d", i);
    107                 }
    108              }
    109              puts("");
    110         }
    111     }
    112     return 0;
    113 }
  • 相关阅读:
    服务器Jmail配置问题
    Silverlight视频教程、资源下载。如果你觉得看图文不够形象,不够生动,那就看看视频吧。
    AspNetPager,asp.net分页的最终解决方案!
    VS2008的Web Application——net 1.1 CodeBehind 模式的回归(非编译模式)
    修复Jscript(IE浏览器脚本引擎)异常
    SQL Server中查看SQL句子执行所用的时间
    SilverLight开发系列第1步:搭建开发环境
    SilverLight开发系列第2步:使用vs2008和Blend 2.5打造Hellow World程序
    谨慎使用Paypal一类的 支付 中介公司
    一个典型的数据库操作事务死锁分析
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5717213.html
Copyright © 2011-2022 走看看