zoukankan      html  css  js  c++  java
  • poj 3463 最短路与次短路的方案数求解

    Sightseeing
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 8968   Accepted: 3139

    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 A, B and L, separated by single spaces, with 1 ≤ A, BN, AB 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, FN and SF: 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

    题意:
    题解:
      1 /******************************
      2 code by drizzle
      3 blog: www.cnblogs.com/hsd-/
      4 ^ ^    ^ ^
      5  O      O
      6 ******************************/
      7 //#include<bits/stdc++.h>
      8 #include<map>
      9 #include<set>
     10 #include<cmath>
     11 #include<queue>
     12 #include<bitset>
     13 #include<math.h>
     14 #include<vector>
     15 #include<string>
     16 #include<stdio.h>
     17 #include<cstring>
     18 #include<iostream>
     19 #include<algorithm>
     20 //#pragma comment(linker, "/STACK:102400000,102400000")
     21 using namespace std;
     22 #define  A first
     23 #define B second
     24 const int mod=1000000007;
     25 const int MOD1=1000000007;
     26 const int MOD2=1000000009;
     27 const double EPS=0.00000001;
     28 typedef __int64 ll;
     29 const ll M22OD=1000000007;
     30 const int INF=1000000010;
     31 const ll MAX=1ll<<55;
     32 const double eps=1e-14;
     33 const double inf=~0u>>1;
     34 const double pi=acos(-1.0);
     35 typedef double db;
     36 typedef unsigned int uint;
     37 typedef unsigned long long ull;
     38 struct node
     39 {
     40     int v,next,w;
     41 }edge[500005];
     42 int d[1005][2],e,n,m;
     43 int cnt[1005][2];
     44 int head[1005];
     45 bool vis[1005][2];
     46 void init()
     47 {
     48     e=0;
     49     memset(head,0,sizeof(head));
     50 }
     51 void insert(int x,int y,int w)
     52 {
     53     e++;
     54     edge[e].v=y;
     55     edge[e].w=w;
     56     edge[e].next=head[x];
     57     head[x]=e;
     58 }
     59 int dijkstra(int s,int t)
     60 {
     61     int flag,u;
     62     memset(vis,0,sizeof(vis));
     63     memset(cnt,0,sizeof(cnt));
     64     for(int i=1;i<=n;i++){
     65         d[i][0]=d[i][1]=INF;
     66     }
     67     cnt[s][0]=1;
     68     d[s][0]=0;
     69     for(int i=1;i<=2*n;i++)
     70     {
     71         int mini=INF;
     72         for(int j=1;j<=n;j++)
     73         {
     74             if(!vis[j][0]&&d[j][0]<mini)
     75             {
     76                 u=j;
     77                 flag=0;
     78                 mini=d[j][0];
     79             }
     80             else if(!vis[j][1]&&d[j][1]<mini)
     81             {
     82                 u=j;
     83                 flag=1;
     84                 mini=d[j][1];
     85             }
     86         }
     87         if(mini==INF) break;
     88         vis[u][flag]=1;
     89         for(int j=head[u];j;j=edge[j].next)
     90         {
     91             int w=edge[j].w;
     92             int v=edge[j].v;
     93             if(d[v][0]>mini+w){
     94                 d[v][1]=d[v][0];
     95                 cnt[v][1]=cnt[v][0];
     96                 d[v][0]=mini+w;
     97                 cnt[v][0]=cnt[u][flag];
     98             }
     99             else if(d[v][0]==mini+w) cnt[v][0]+=cnt[u][flag];
    100             else if(d[v][1]>mini+w){
    101                 d[v][1]=mini+w;
    102                 cnt[v][1]=cnt[u][flag];
    103             }
    104             else if(d[v][1]==mini+w) cnt[v][1]+=cnt[u][flag];
    105         }
    106     }
    107     int ans=0;
    108     if(d[t][1]==d[t][0]+1) ans=cnt[t][1]+cnt[t][0];
    109     else  ans=cnt[t][0];
    110     return ans;
    111 }
    112 int main()
    113 {
    114     int s,t, T,x,y,w;
    115     scanf("%d",&T);
    116      while(T--)
    117      {
    118          init();
    119          scanf("%d %d",&n,&m);
    120          for(int i=1;i<=m;i++)
    121          {
    122              scanf("%d %d %d",&x,&y,&w);
    123              insert(x,y,w);
    124          }
    125          scanf("%d %d",&s,&t);
    126          printf("%d
    ",dijkstra(s,t));
    127      }
    128     return 0;
    129 }
  • 相关阅读:
    1115:直方图
    1114:白细胞计数
    1114:白细胞计数
    忘记mysql密码后重置密码
    pycharm中无法导入pip安装的包
    win7升级win10
    WIN7 Windows update提示不支持硬件
    getcwd() 和 os.path.realpath () 的区别
    selenium启动chrome时,弹出设置页面:Windows Defender 防病毒要重置您的设置
    ui测试如何检测页面差异
  • 原文地址:https://www.cnblogs.com/hsd-/p/6028563.html
Copyright © 2011-2022 走看看