zoukankan      html  css  js  c++  java
  • POJ 3463 Sightseeing dijkstra

    Sightseeing
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 6415   Accepted: 2265

    Description

    Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.

    Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.

    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.

    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 AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ 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 ≤ SF ≤ 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.

    Source


    ------------------

    dijkstra最短路推广到次短路。可扩展到更短路。

    ------------------

    /** head-file **/
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <list>
    #include <set>
    #include <map>
    #include <algorithm>
    
    /** define-for **/
    
    #define REP(i, n) for (int i=0;i<int(n);++i)
    #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
    #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
    #define REP_1(i, n) for (int i=1;i<=int(n);++i)
    #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
    #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
    #define REP_N(i, n) for (i=0;i<int(n);++i)
    #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
    #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
    #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
    #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
    #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
    
    /** define-useful **/
    
    #define clr(x,a) memset(x,a,sizeof(x))
    #define sz(x) int(x.size())
    #define see(x) cerr<<#x<<" "<<x<<endl
    #define se(x) cerr<<" "<<x
    #define pb push_back
    #define mp make_pair
    
    /** test **/
    
    #define Display(A, n, m) {                      
        REP(i, n){                                  
            REP(j, m) cout << A[i][j] << " ";       
            cout << endl;                           
        }                                           
    }
    
    #define Display_1(A, n, m) {                    
        REP_1(i, n){                                
            REP_1(j, m) cout << A[i][j] << " ";     
            cout << endl;                           
        }                                           
    }
    
    using namespace std;
    
    /** typedef **/
    
    typedef long long LL;
    
    /** Add - On **/
    
    const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
    const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
    const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
    
    const int MOD = 1000000007;
    const int INF = 0x3f3f3f3f;
    const long long INFF = 1LL << 60;
    const double EPS = 1e-9;
    const double OO = 1e15;
    const double PI = acos(-1.0); //M_PI;
    
    const int maxn=1111;
    const int maxm=31111;
    
    struct EdgeNode{
        int to;
        int w;
        int next;
    };
    
    struct HeapNode{
        int d,u;
        bool bs;
        bool operator<(const HeapNode& rhs) const{
            return d>rhs.d;
        }
    };
    
    struct Dijkstra{
        EdgeNode edges[maxm];
        int head[maxn];
        int edge,n;
        void init(int n){
            this->n=n;
            memset(head,-1,sizeof(head));
            edge=0;
        }
        void addedges(int u,int v,int c){
            edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
        bool done[maxn][2];
        int dis[maxn][2];
        int num[maxn][2];
        int dijkstra(int s,int t){
            priority_queue<HeapNode>que;
            memset(done,0,sizeof(done));
            memset(num,0,sizeof(num));
            for (int i=0;i<=n;i++){
                dis[i][0]=INF;
                dis[i][1]=INF;
            }
            dis[s][0]=0;
            num[s][0]=1;
            que.push((HeapNode){0,s,0});
            while (!que.empty()){
                HeapNode x=que.top();
                que.pop();
                int u=x.u;
                bool bs=x.bs;
                if (done[u][bs]) continue;
                done[u][bs]=true;
                for (int i=head[u];i!=-1;i=edges[i].next){
                    int v=edges[i].to;
                    int nw=edges[i].w+dis[u][bs];
                    if (dis[v][0]>nw){
                        if (dis[v][0]!=INF){
                            dis[v][1]=dis[v][0];
                            num[v][1]=num[v][0];
                            que.push((HeapNode){dis[v][1],v,1});
                        }
                        dis[v][0]=nw;
                        num[v][0]=num[u][bs];
                        que.push((HeapNode){dis[v][0],v,0});
                    }
                    else if (dis[v][0]==nw){
                        num[v][0]+=num[u][bs];
                    }
                    else if (dis[v][1]>nw){
                        dis[v][1]=nw;
                        num[v][1]=num[u][bs];
                        que.push((HeapNode){dis[v][1],v,1});
                    }
                    else if (dis[v][1]==nw){
                        num[v][1]+=num[u][bs];
                    }
                }
            }
            if (dis[t][0]+1==dis[t][1])
            {
                return num[t][0]+num[t][1];
            }
            else
            {
                return num[t][0];
            }
        }
    }solver;
    
    int n,m;
    
    int main()
    {
        int T,s,f;
        scanf("%d",&T);
        while (T--)
        {
            scanf("%d%d",&n,&m);
            solver.init(n);
            while (m--)
            {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                solver.addedges(x,y,w);
            }
            scanf("%d%d",&s,&f);
            int ans=solver.dijkstra(s,f);
            printf("%d
    ",ans);
        }
        return 0;
    }
    



  • 相关阅读:
    双循环解决添加列表问题
    贪心算法
    隔板法发红包
    python小兵之时间模块
    开发规范
    python 小兵(12)模块1
    Linux系统
    刷题
    Socket
    栈和队列
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681646.html
Copyright © 2011-2022 走看看