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 }
  • 相关阅读:
    JS的中数的取值范围的大小
    前端通过xlsx插件导入excel
    H5和安卓原生进行交互的操作流程记录
    javascript中字符串和数字之间互相转换的方法总结
    gitlab代码合并到主分支
    typeof和valueof、instance of 之间的区别
    javascript中map会改变原始的数组吗
    使用typescript来写react遇到的一些问题
    使用javascript进行时间数据格式的转换
    在vue的移动端项目中使用vue-echarts
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5717213.html
Copyright © 2011-2022 走看看