• HDU-4725 The Shortest Path in Nya Graph( 最短路 )


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725

    Problem Description
    This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
    The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
    You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
    Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
    Help us calculate the shortest path from node 1 to node N.
     
    Input
    The first line has a number T (T <= 20) , indicating the number of test cases.
    For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
    The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
    Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.
     
    Output
    For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
    If there are no solutions, output -1.
     
    Sample Input
    2
    3 3 3
    1 3 2
    1 2 1
    2 3 1
    1 3 3
     
    3 3 3
    1 3 2
    1 2 2
    2 3 2
    1 3 4
     
    Sample Output
    Case #1: 2
    Case #2: 3
     
    给若干点,分布于若干层,同层的点是不可以直接到达的,邻层的点可以相互到达,花费为c,同时给了若干无向路,求1到n的最小花费
    题目难点在于建图,要将层抽象为边的关系,于是我们可以假设出2 * n个点,每层一个进点,一个出点,进点有到该层的实点和相邻层的出点到它的有向路,出点反之
    本题去了一个坑点,题意上相邻层若没点是不可互通的,也就是说我们需要考虑一个无点层两侧的点的关系,但是题目所给的输入时排除了这种情况的
    最后居然因为数组开小了T了好久T_T
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<map>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<stack>
     9 
    10 using namespace std;
    11 
    12 const int INF = 0x3f3f3f3f;
    13 const int MAX = 100005 * 10;
    14 
    15 struct Edge{
    16     int to, val, next;
    17 }edge[MAX];
    18 
    19 struct Node{
    20     int x, d;
    21     Node(){}
    22     Node( int a, int b ) { x = a; d = b; }
    23     bool operator < ( const Node & a ) const{
    24         return d > a.d;
    25     }
    26 };
    27 
    28 int n, m, c, cnt, h[MAX], dis[MAX];
    29 
    30 void add( int beg, int end, int val ){
    31     edge[cnt].to = end;
    32     edge[cnt].val = val;
    33     edge[cnt].next = h[beg];
    34     h[beg] = cnt;
    35     cnt++;
    36 }
    37 
    38 void dij( int start ){
    39     memset( dis, INF, sizeof( dis ) );
    40     dis[start] = 0;
    41 
    42     priority_queue<Node> Q;
    43     Q.push( Node( start, dis[start] ) );
    44 
    45     while( !Q.empty() ){
    46         Node node = Q.top(); Q.pop();
    47         int x = node.x;
    48 
    49         if( dis[x] < node.d ) continue;
    50         for( int k = h[x]; k != 0; k = edge[k].next ){
    51             int y = edge[k].to;
    52             if( dis[y] > dis[x] + edge[k].val ){
    53                 dis[y] = dis[x] + edge[k].val;
    54                 Q.push( Node( y, dis[y] ) );
    55             }
    56         }
    57     }
    58 }
    59 
    60 //层的入点为n + r
    61 //层的出点为2 * n + r
    62 
    63 int main(){
    64     int T;
    65     scanf( "%d", &T );
    66     for( int t = 1; t <= T; t++ ){
    67         scanf( "%d%d%d", &n, &m, &c );
    68 
    69         int r, beg, end, val;
    70         
    71         cnt = 1;
    72         memset( h, 0, sizeof( h ) );
    73         for( int i = 1; i <= n; i++ ){
    74             scanf( "%d", &r );
    75             add( n + r, i, 0 );
    76             add( i, n * 2 + r, 0 );
    77         }
    78         for( int i = 0; i < m; i++ ){
    79             scanf( "%d%d%d", &beg, &end, &val );
    80             add( beg, end, val );
    81             add( end, beg, val );
    82         }
    83         for( int i = 1; i < n; i++ ){
    84             add( n * 2 + i, n + i + 1, c );
    85             add( n * 2 + i + 1, n + i, c );
    86         }
    87 
    88         dij( 1 );
    89 
    90         cout << "Case #" << t << ": ";
    91         if( dis[n] == INF ) cout << -1 << endl;
    92         else cout << dis[n] << endl;
    93     }
    94 
    95     return 0;
    96 }
  • 相关阅读:
    Linux 系统中用户切换(su user与 su
    linux 用户打开进程数和文件数调整
    hive sql 语法详解
    iOS
    iOS
    MySQL的事务的处理
    iOS
    iOS AOP编程思想及实践
    iOS 静态库和动态库(库详解)
    iOS 沙盒目录结构及正确使用
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5674607.html
Copyright © 2020-2023  润新知