zoukankan      html  css  js  c++  java
  • HDU 3191 How Many Paths Are There

    How Many Paths Are There

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3191
    64-bit integer IO format: %I64d      Java class name: Main
     
      oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
      One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
      Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
      You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.
     

    Input

    There are some cases. Proceed till the end of file.
    The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
    N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
    Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y. 
    All the nodes are marked from 0 to N-1.
     

    Output

    For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
     

    Sample Input

    3 3 0 2
    0 2 5
    0 1 4
    1 2 2

    Sample Output

    6 1

    Source

     
    解题:Dijkstra求次短路
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 const int maxn = 55;
    18 struct arc {
    19     int to,w,next;
    20     arc(int x = 0,int y = 0,int z = -1) {
    21         to = x;
    22         w = y;
    23         next = z;
    24     }
    25 };
    26 arc e[10000];
    27 int head[maxn],d[maxn][2],cnt[maxn][2],tot,big,ed,n,m;
    28 bool vis[maxn][2];
    29 void add(int u,int v,int w){
    30     e[tot] = arc(v,w,head[u]);
    31     head[u] = tot++;
    32 }
    33 void dijkstra(){
    34     for(int i = 0; i <= n; i++){
    35         vis[i][0] = vis[i][1] = false;
    36         cnt[i][0] = cnt[i][1] = 0;
    37         d[i][0] = d[i][1] = INF;
    38     }
    39     d[big][0] = 0;
    40     cnt[big][0] = 1;
    41     while(true){
    42         int u,Min = INF,flag;
    43         for(int i = 0; i < n; i++){
    44             if(!vis[i][0] && d[i][0] < Min)
    45                 Min = d[u = i][flag = 0];
    46             else if(!vis[i][1] && d[i][1] < Min)
    47                 Min = d[u = i][flag = 1];
    48         }
    49         if(u == ed && flag || Min == INF) break;
    50         vis[u][flag] = true;
    51         for(int i = head[u]; ~i; i = e[i].next){
    52             int v = e[i].to;
    53             int w = d[u][flag] + e[i].w;
    54             if(d[v][0] > w){
    55                 if(d[v][0] < INF){
    56                     d[v][1] = d[v][0];
    57                     cnt[v][1] = cnt[v][0];
    58                 }
    59                 d[v][0] = w;
    60                 cnt[v][0] = cnt[u][flag];
    61             }else if(d[v][0] == w)
    62                 cnt[v][0] += cnt[u][flag];
    63             else if(d[v][1] > w){
    64                 d[v][1] = w;
    65                 cnt[v][1] = cnt[u][flag];
    66             }else if(d[v][1] == w) cnt[v][1] += cnt[u][flag];
    67         }
    68     }
    69     printf("%d %d
    ",d[ed][1],cnt[ed][1]);
    70 }
    71 int main(){
    72     int u,v,w;
    73     while(~scanf("%d %d %d %d",&n,&m,&big,&ed)){
    74         memset(head,-1,sizeof(head));
    75         for(int i = tot = 0; i < m; i++){
    76             scanf("%d %d %d",&u,&v,&w);
    77             add(u,v,w);
    78         }
    79         dijkstra();
    80     }
    81     return 0;
    82 }
    View Code
  • 相关阅读:
    Ajax请求过程中显示“进度”的简单实现
    Asp.net 图片文件防盗链介绍
    ASP.NET MVC验证
    MVC文件上传
    MVC文件上传-使用jQuery.FileUpload和Backload组件实现文件上传
    使用jQuery.FileUpload插件和服Backload组件自定义上传文件夹
    使用jQuery.FileUpload和Backload自定义控制器上传多个文件
    使用jQuery.FileUpload插件和Backload组件裁剪上传图片
    CSS3 多列
    CSS3 2D转换 动画
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4001145.html
Copyright © 2011-2022 走看看