HDU_3191_How Many Paths Are There

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1210    Accepted Submission(s): 411

Problem Description

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

题意:给你一个有向图,求出次短路长度和条数。

分析:借鉴http://www.cnblogs.com/ziyi--caolu/p/3470073.html

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

代码清单:

#include<map>
#include<queue>
#include<stack>
#include<ctime>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 50 + 5;
const int max_dis = 1e9;

struct Edge{
    int to;
    int dis;
    Edge(int to,int dis){
        this -> to = to;
        this -> dis = dis;
    }
};

struct Node{
    int d;
    int v;
    int pose;    // 0 代表最短路   1 代表次短路
    friend bool operator<(Node x,Node y){
        if(x.d==y.d) return x.v>y.v;
        return x.d>y.d;
    }
};

int N,M,S,E,a,b,c;
bool vis[maxn][2];
int dist[maxn][2];
int degree[maxn][2];
vector<Edge>G[maxn];

void dij(int s,int e){

    for(int i=0;i<maxn;i++){
        dist[i][0]=dist[i][1]=max_dis;
        degree[i][0]=degree[i][1]=0;
        vis[i][0]=vis[i][1]=false;
    }
    priority_queue<Node>q;
    while(q.size()) q.pop();
    Node p,w;
    p.d=0; p.v=s; p.pose=0;
    degree[p.v][p.pose]=1;
    q.push(p);
    while(q.size()){
        p=q.top(); q.pop();
        if(vis[p.v][p.pose]) continue;
        vis[p.v][p.pose]=true;
        for(int i=0;i<G[p.v].size();i++){

            Edge& e=G[p.v][i];
            if(!vis[e.to][0]&&p.d+e.dis<dist[e.to][0]){  //找到一条比当前最短路更短的路
                if(dist[e.to][0]!=max_dis){              //作为次短路,入队
                    w.v=e.to; w.d=dist[e.to][0]; w.pose=1;
                    dist[e.to][1]=dist[e.to][0];
                    degree[e.to][1]=degree[e.to][0];
                    q.push(w);
                }
                w.v=e.to; w.d=p.d+e.dis; w.pose=0;       //更新最短路,入队
                dist[e.to][0]=w.d; degree[e.to][0]=degree[p.v][p.pose];
                q.push(w);
            }

            else if(!vis[e.to][0]&&p.d+e.dis==dist[e.to][0]){ //找到一条相同距离的最短路,更新条数,不入队
                degree[e.to][0]+=degree[p.v][p.pose];
            }

            else if(!vis[e.to][1]&&p.d+e.dis<dist[e.to][1]){ //找到一条比当前次短路更短的路,不可以更新最短路,可更新次短路,入队
                w.v=e.to; w.d=p.d+e.dis; w.pose=1;
                dist[e.to][1]=w.d; degree[e.to][1]=degree[p.v][p.pose];
                q.push(w);
            }

            else if(!vis[e.to][1]&&p.d+e.dis==dist[e.to][1]){ //找到一条相同距离的次短路,更新条数,不入队
                degree[e.to][1]+=degree[p.v][p.pose];
            }
        }
    }
}

int main(){
    while(scanf("%d%d%d%d",&N,&M,&S,&E)!=EOF){
        for(int i=0;i<maxn;i++) G[i].clear();
        for(int i=0;i<M;i++){
            scanf("%d%d%d",&a,&b,&c);
            G[a].push_back(Edge(b,c));
        }
        dij(S,E);
        printf("%d %d\n",dist[E][1],degree[E][1]);
    }return 0;
}
时间: 2024-11-01 02:07:44

HDU_3191_How Many Paths Are There的相关文章

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma

62.Unique Paths (法1递归-动态规划法2数学公式)

A robot is located at the top-left corner of a m x n grid(marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. Therobot is trying to reach the bottom-right corner of the grid (marked 'Finish'in the

[LeetCode]Unique Paths

题目: 从左上角到右下角的所有可能路径. 思路1: 回溯法去递归遍历所有的路径,但是复杂度太大,无法通过.checkPath方法实现 动态规划法,从左上角到每一格的路径数与它的上面一格和左边一格的路径和: N(m,n)=N(m-1,n)+N(m,n-1): 注意:第一行和第一列的特殊情况. package com.example.medium; /** * A robot is located at the top-left corner of a m x n grid (marked 'Sta

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

LintCode : Unique Paths II

Problem Description: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. Code: public class Solutio

LintCode : Unique Paths

Problem Description: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid

62. 63. Unique Paths 64. Minimum Path Sum

1. A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' i

[LeetCode257]Binary Tree Paths

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 求一个二叉树的所有路径 代码: /** * Definition for a binary tree node. * struct