[次短路][路径计数](模板题) poj3436 Sightseeing

来源:互联网 发布:bgm网络语什么意思 编辑:程序博客网 时间:2024/06/01 10:21

@(ACM题目)[图论, 差分约束]

Description

…(旅游团要在有向图上旅游)
There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.
Alt text
For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.
Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
- One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.
- M lines, each with three integers A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.
- The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.
- One line with two integers S and F, separated by a single space, with 1 ≤ S, F ≤ N and S ≠ F: the starting city and the final city of the route.

There will be at least one route from S to F.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109=1,000,000,000

Sample Input

2
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1

Sample Output

3
2

Hint

The first test case above corresponds to the picture in the problem description.

题目分析

求最短路路径数,若次短路长度比最短路多1,再加上次短路路径数。
模板题,使用Dijkstra略加修改即可,详见代码。

代码

#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;typedef long long LL;const int maxn = 1e5 + 5;//顶点数const int maxm = 1e5 + 5;//边数const int inf = 0x3f3f3f3f;//const LL inf = 0x3f3f3f3f3f3f3f3f;struct Dijstra//d2[t] < inf代表有解{    int n, m;    int d[2][maxn];//LL,d[0]记录最短路,d[1]记录次短路    int cnt[2][maxn];//cnt[0]记录最短路条数,cnt[1]记录次短路径条数    int head[maxn];    //int par[maxn];//记录路径    struct Edge    {        int to, nxt;        int dis;//LL    }e[maxm];    struct Node    {        int f;//0代表最短路,1代表次短路        int u;//结点编号        int dis;//LL        bool operator<(const Node &rhs) const        {            return dis > rhs.dis;        }    };    void init(int nn)    {        n = nn;        m = 0;        memset(head, 0xff, sizeof head);    }    void addEdge(int from, int to, int dis)//LL dis    {        e[m].to = to;        e[m].dis = dis;        e[m].nxt = head[from];        head[from] = m++;    }    void finda(int s)    {        priority_queue<Node> q;        for(int i = 0; i < n; ++i) d[0][i]  = d[1][i] = inf;//0~n-1,注意下标从哪里开始        d[0][s] = 0;        for(int i = 0; i < n; ++i) cnt[0][i] = cnt[1][i] = 0;        cnt[0][s] = 1;        q.push((Node){0, s, 0});        while(!q.empty())        {            Node x = q.top(); q.pop();            int u = x.u;            int dis = x.dis;            int f = x.f;            if(d[f][u] < dis) continue;            for(int i = head[u]; ~i; i = e[i].nxt)            {                int v = e[i].to;                int dd = dis + e[i].dis;//LL                if(d[0][v] > dd)//能更新最短路就先更新最短路                {                    if(d[0][v] < inf)//之前的最短路变为次短路                    {                        d[1][v] = d[0][v];                        cnt[1][v] = cnt[0][v];                        q.push((Node){1, v, d[1][v]});                    }                    //更新最短路                    d[0][v] = dd;                    cnt[0][v] = cnt[f][u];                    q.push((Node){0, v, d[0][v]});                }                else if(d[0][v] == dd)//更新最短路条数                {                    cnt[0][v] += cnt[f][u];                }                else if(d[1][v] > dd)//不能更新最短路,只能更新次短路                {                    d[1][v] = dd;                    cnt[1][v] = cnt[f][u];                    q.push((Node){1, v, d[1][v]});                }                else if(d[1][v] == dd)//更新次短路条数                {                    cnt[1][v] += cnt[f][u];                }            }        }    }}a;int main(){    int T;    cin >> T;    while(T--)    {        int n, m;        scanf("%d%d", &n, &m);        a.init(n);        int u, v, w;        for(int i = 0; i < m; ++ i)        {            scanf("%d%d%d", &u, &v, &w);            a.addEdge(u-1, v-1, w);        }        int s, t;        scanf("%d%d", &s, &t);        --s;        --t;        a.finda(s);        int res = a.cnt[0][t];        if(a.d[1][t] - 1 == a.d[0][t]) res += a.cnt[1][t];        printf("%d\n", res);    }    return 0;}
原创粉丝点击