HDU1688--Sightseeing(最短路与次短路的条数和)

Sightseeing
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 520 Accepted Submission(s): 210


Problem 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, 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 10^9 = 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


Source
华东区大学生程序设计邀请赛_热身赛

 

  1 //此次短路是在理解别人的模板上再写出来的
  2 #include<cstdio>
  3 #include<iostream>
  4 #include<cstring>
  5 #include<cmath>
  6 #include<algorithm>
  7 using namespace std;
  8 
  9 struct Node
 10 {
 11     int v,w;
 12     int next;
 13 }fpoint[10001];
 14 
 15 #define INF 99999999
 16 int dis[1001][2];              //二维,一维表示最短路,一维表示次短路
 17 int dp[1001][2];              //来记录数量
 18 int vis[1001][2];
 19 int n,m;
 20 int head[1001];
 21 int index;
 22 int st,se;
 23 
 24 void init()
 25 {
 26     int i;
 27     memset(head,0xff,sizeof(head));
 28     for(i=1;i<=n;i++)
 29     {
 30         dis[i][0]=dis[i][1]=INF;
 31         vis[i][0]=vis[i][1]=0;
 32         dp[i][0]=dp[i][1]=0;
 33     }
 34 }
 35 
 36 void addedge(int x,int y,int z)
 37 {
 38     fpoint[index].v=y;
 39     fpoint[index].w=z;
 40     fpoint[index].next=head[x];
 41     head[x]=index++;
 42 }
 43 
 44 int dijkstra()
 45 {
 46     int i,j;
 47     dis[st][0]=0;
 48     dp[st][0]=1;
 49     for(i=1;i<=2*n;i++)
 50     {
 51         int mmin=INF;
 52         int x=-1;
 53         int flag;
 54         for(j=1;j<=n;j++)
 55         {
 56             if(vis[j][0]==0&&dis[j][0]<mmin)
 57             {
 58                 mmin=dis[j][0];
 59                 x=j;
 60                 flag=0;
 61             }
 62             if(vis[j][1]==0&&dis[j][1]<mmin)
 63             {
 64                 mmin=dis[j][1];
 65                 x=j;
 66                 flag=1;
 67             }
 68         }
 69         vis[x][flag]=1;
 70         if(mmin==INF)break;
 71         int t;
 72         for(t=head[x];t!=-1;t=fpoint[t].next)
 73         {
 74             int v=fpoint[t].v;
 75             int len=mmin+fpoint[t].w;
 76             if(len<dis[v][0])
 77             {
 78                 dis[v][1]=dis[v][0];
 79                 dis[v][0]=len;
 80                 dp[v][1]=dp[v][0];
 81                 dp[v][0]=dp[x][flag];
 82             }
 83             else if(len==dis[v][0])
 84             {
 85                 dp[v][0]+=dp[x][flag];
 86             }
 87             else if(len<dis[v][1])
 88             {
 89                 dis[v][1]=len;
 90                 dp[v][1]=dp[x][flag];
 91             }
 92             else  if(len==dis[v][1])
 93             {
 94                 dp[v][1]+=dp[x][flag];
 95             }
 96         }
 97     }
 98     if(dis[se][0]+1==dis[se][1])
 99         return dp[se][0]+dp[se][1];
100     return dp[se][0];
101 }
102 
103 int t;
104 
105 int main()
106 {
107     scanf("%d",&t);  
108     while(t--)
109     {
110         index=1;
111         scanf("%d%d",&n,&m);
112         init();
113         int i;
114         for(i=1;i<=m;i++)
115         {
116             int u,v,w;
117             scanf("%d%d%d",&u,&v,&w);
118             addedge(u,v,w);
119         }
120         scanf("%d%d",&st,&se);
121         int ans=dijkstra();
122         printf("%d\n",ans);
123     }
124     return 0;
125 }
View Code

 

 

 

 

 

 

posted on 2013-07-24 16:06  张狂不年轻°  阅读(258)  评论(0编辑  收藏  举报