POJ 3463 Sightseeing

Sightseeing

Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 3463
64-bit integer IO format: %lld      Java class name: Main
 

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

Source

 

解题:A*居然超时。只好这样搞了。。。。。。。。。

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 1010;
 18 struct arc {
 19     int to,w,next;
 20     arc(int x = 0,int y = 0,int z = -1) {
 21         to = x;
 22         w = y;
 23         next = z;
 24     }
 25 };
 26 struct node{
 27     int v,w;
 28     bool flag;
 29     node(int x = 0,int y = 0,bool z = false){
 30         v = x;
 31         w = y;
 32         flag = z;
 33     }
 34     bool operator < (const node &x) const{
 35         return w > x.w;
 36     }
 37 };
 38 arc e[20010];
 39 int head[maxn],d[maxn][2],cnt[maxn][2],tot,n,m,s,t;
 40 priority_queue<node>q;
 41 void Dijkstra(){
 42     while(!q.empty()) q.pop();
 43     for(int i = 0; i <= n; i++){
 44         d[i][0] = d[i][1] = INF;
 45         cnt[i][0] = cnt[i][1] = 0;
 46     }
 47     cnt[s][0] = 1;
 48     d[s][0] = 0;
 49     q.push(node(s,0,false));
 50     while(!q.empty()){
 51         node now = q.top();
 52         q.pop();
 53         int v = now.v,w = now.w;
 54         bool flag = now.flag;
 55         if(d[v][flag] != w) continue;
 56         for(int i = head[v]; ~i; i = e[i].next){
 57             int u = e[i].to;
 58             int cost = w + e[i].w;
 59             if(d[u][0] > cost){
 60                 if(d[u][0] < INF){
 61                     d[u][1] = d[u][0];
 62                     q.push(node(u,d[u][1],true));
 63                 }
 64                 cnt[u][1] = cnt[u][0];
 65                 cnt[u][0] = cnt[v][flag];
 66                 d[u][0] = cost;
 67                 q.push(node(u,d[u][0],false));
 68                 continue;
 69             }
 70             if(d[u][0] == cost){
 71                 cnt[u][0] += cnt[v][flag];
 72                 continue;
 73             }
 74             if(d[u][1] > cost){
 75                 d[u][1] = cost;
 76                 cnt[u][1] = cnt[v][flag];
 77                 q.push(node(u,d[u][1],true));
 78                 continue;
 79             }
 80             if(d[u][1] == cost) cnt[u][1] += cnt[v][flag];
 81         }
 82     }
 83 }
 84 void add(int u,int v,int w) {
 85     e[tot] = arc(v,w,head[u]);
 86     head[u] = tot++;
 87 }
 88 int main(){
 89     int ks,u,v,w;
 90     scanf("%d",&ks);
 91     while(ks--){
 92         memset(head,-1,sizeof(head));
 93         scanf("%d %d",&n,&m);
 94         for(int i = tot = 0; i < m; i++){
 95             scanf("%d %d %d",&u,&v,&w);
 96             add(u,v,w);
 97         }
 98         scanf("%d %d",&s,&t);
 99         Dijkstra();
100         printf("%d\n",d[t][0] + 1 == d[t][1]?cnt[t][0]+cnt[t][1]:cnt[t][0]);
101     }
102     return 0;
103 }
View Code

 

posted @ 2014-09-21 22:41  狂徒归来  阅读(209)  评论(0编辑  收藏  举报