您的位置:首页 > 其它

HDU_3191_How Many Paths Are There(次短路条数)

2015-04-16 21:11 423 查看

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: