HDU 3191 How Many Paths Are There【次短路数量】

http://acm.hdu.edu.cn/showproblem.php?pid=3191
How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1814 Accepted Submission(s): 643

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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e3 + 10;
int cnt, head[maxn];
int dis[maxn][2], dp[maxn][2], vis[maxn][2];
struct node {
    int u, d, fl;
    node(int _u = 0, int _d = 0, int _fl = 0) :u(_u), d(_d), fl(_fl) {}
    bool operator < (const node &rhs) const {
        if (d != rhs.d) return d > rhs.d;
        return u > rhs.u;
    }
};
struct edge {
    int to, w, nx;
}e[maxn<<1];
void init() {
    memset(head, -1, sizeof head);
    cnt = 0;
}
void add(int u, int v, int w)
{
    e[cnt].to = v;
    e[cnt].w = w;
    e[cnt].nx = head[u];
    head[u] = cnt++;
}
void dijkstra(int s, int t) {
    memset(dp, 0, sizeof dp);
    memset(vis, 0, sizeof vis);
    memset(dis, INF, sizeof dis);
    priority_queue <node> q;
    dis[s][0] = 0; dp[s][0] = 1;
    q.push(node(s, 0, 0));
    while (!q.empty()) {
        node cur = q.top();
        q.pop();
        int u = cur.u;
        int fl = cur.fl;
        if (vis[u][fl]) continue; vis[u][fl] = 1;
        for (int i = head[u]; ~i; i = e[i].nx) {
            node nxt;
            int v = e[i].to;
            int w = e[i].w;
            if (!vis[v][0] && cur.d + w < dis[v][0]) {
                if (dis[v][0] != INF) {
                    nxt.u = v; nxt.d = dis[v][0]; nxt.fl = 1;//次短路被当前最短路替代
                    dis[v][1] = dis[v][0];
                    dp[v][1] = dp[v][0];
                    q.push(nxt);
                }
                dis[v][0] = cur.d + w;//当前最短路被更优秀的最短路替代
                dp[v][0] = dp[u][fl];
                nxt.u = v; nxt.d = dis[v][0]; nxt.fl = 0;
                q.push(nxt);
            }
            else if (!vis[v][0] && cur.d + w == dis[v][0]) {
                dp[v][0] += dp[u][fl];
            }
            else if (!vis[v][1] && cur.d + w < dis[v][1]) {
                dis[v][1] = cur.d + w;
                dp[v][1] = dp[u][fl];
                nxt.u = v; nxt.d = dis[v][1]; nxt.fl = 1;
                q.push(nxt);
            }
            else if (!vis[v][1] && cur.d + w == dis[v][1]) {
                dp[v][1] += dp[u][fl];
            }
        }
    }
}
int main()
{
    int n, m, st, ed;
    while (~scanf("%d %d %d %d", &n, &m, &st, &ed)) {
        init();
        for (int i = 0; i < m; i++) {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            add(u, v, w);
        }
        dijkstra(st, ed);
        printf("%d %d\n", dis[ed][1], dp[ed][1]);
    }
    return 0;
}
//_CRT_SECURE_NO_WARNINGS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值