zoukankan      html  css  js  c++  java
  • hdu 4725

    The Shortest Path in Nya Graph

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2296    Accepted Submission(s): 561


    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
     
    Source
     
    Recommend
    zhuyuanchen520   |   We have carefully selected several similar problems for you:  4822 4821 4820 4819 4818 
     
    最短路
    把每一层拆成2 个点 一个是连接结点入边,一个是连接结点出边,每层连接结点入边的点连接下个层连接结点出边的点,下一层连接入边的点连接上一层连接出边的点。
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 #include <vector>
      6 #include <queue>
      7 
      8 using namespace std;
      9 
     10 const int MAX_N = 3e5 + 7;
     11 const int INF = 1e9 + 7;
     12 typedef long long ll;
     13 struct heapnode {
     14         int d;
     15         int  u;
     16         bool operator < (const heapnode &rhs) const {
     17                 return d > rhs.d;
     18         }
     19 };
     20 struct Edge {int from, to, cost;};
     21 
     22 vector<int> G[MAX_N];
     23 vector<Edge> edges;
     24 int N,M,C;
     25 int d[MAX_N];
     26 bool done[MAX_N];
     27 vector <int> lay[MAX_N];
     28 
     29 void add_edge(int from, int to, int cost) {
     30         edges.push_back((Edge){from, to, cost});
     31         int m = edges.size();
     32         G[from].push_back(m - 1);
     33 }
     34 
     35 void dij(int s) {
     36         memset(done,0,sizeof(done));
     37         for(int i = 1; i <= 3 * N; ++i) {
     38                 d[i] = INF;
     39         }
     40         d[s] = 0;
     41         priority_queue <heapnode> q;
     42         q.push((heapnode) {d[s], s});
     43 
     44         while(!q.empty()) {
     45                 heapnode x = q.top(); q.pop();
     46                 int u = x.u;
     47                 if(done[u]) continue;
     48                 done[u] = 1;
     49                 if(u == N) return;
     50 
     51                 for(int i = 0; i < G[u].size(); ++i) {
     52                         Edge &e = edges[ G[u][i] ];
     53                         if(d[e.to] > d[u] + e.cost) {
     54                                 d[e.to] = d[u] + e.cost;
     55                                 q.push((heapnode) {d[e.to], e.to});
     56                         }
     57                 }
     58         }
     59 }
     60 int main()
     61 {
     62 
     63    // freopen("sw.in","r",stdin);
     64     int t;
     65     scanf("%d",&t);
     66     int ca = 1;
     67     while(t--) {
     68             scanf("%d%d%d",&N,&M,&C);
     69             for(int i = 1; i <= 3 * N; ++i) G[i].clear();
     70             edges.clear();
     71 
     72             for(int i = 1; i <= N; ++i) {
     73                     int ch;
     74                     scanf("%d",&ch);
     75                     add_edge(i, ch + N, 0);
     76                     add_edge(ch + 2 * N, i, 0);
     77 
     78             }
     79 
     80             /*for(int i = 2 * N + 1; i <= 3 * N; ++i) {
     81                     add_edge(i, i - N, 0);
     82             }*/
     83 
     84 
     85             for(int i = N + 1; i <= 2 * N - 1; ++i) {
     86                         add_edge(i, i + 1 + N, C);
     87                         add_edge(i + 1, i + N, C);
     88             }
     89 
     90             for(int i = 1; i <= M; ++i) {
     91                     int u, v, w;
     92                     scanf("%d%d%d",&u, &v, &w);
     93                     add_edge(u, v, w);
     94                     add_edge(v, u, w);
     95             }
     96 
     97             dij(1);
     98             printf("Case #%d: %d
    ",ca++, (d[N] == INF) ? -1 : d[N]);
     99     }
    100 
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    Spring Security -- 添加图形验证码(转载)
    Spring Security -- 自定义用户认证(转载)
    pandas agg 后降低df表层
    判断是否有人跟踪车辆的方案
    格隆汇笔记-黄勇演讲
    Mac mysql 忘记root密码的解决方法
    sql 同步2个表中的一个字段数据
    Linux下配置用msmtp和mutt发邮件
    spark StructType的应用,用在处理mongoDB keyvalue
    Idea2018旗舰版破解方法
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3715305.html
Copyright © 2011-2022 走看看