zl程序教程

您现在的位置是:首页 >  其它

当前栏目

POJ 1847 Tram

poj
2023-09-11 14:15:28 时间

Tram

Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 1847
64-bit integer IO format: %lld      Java class name: Main
Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch. 

When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually. 

Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B. 
 

Input

The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N. 

Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 
 

Output

The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".
 

Sample Input

3 2 1
2 2 3
2 3 1
2 1 2

Sample Output

0

Source

 

解题:直接最短路,非初始状态的都需要费用为1

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #define pii pair<int,int>
 6 using namespace std;
 7 const int maxn = 110;
 8 const int INF = 0x3f3f3f3f;
 9 struct arc{
10     int to,w,next;
11     arc(int x = 0,int y = 0,int z = -1){
12         to = x;
13         w = y;
14         next = z;
15     }
16 }e[maxn*maxn];
17 int head[maxn],d[maxn],tot;
18 void add(int u,int v,int w){
19     e[tot] = arc(v,w,head[u]);
20     head[u] = tot++;
21 }
22 bool done[maxn];
23 int dijkstra(int s,int t){
24     priority_queue< pii,vector< pii >,greater< pii > >q;
25     memset(d,0x3f,sizeof d);
26     memset(done,false,sizeof done);
27     q.push(pii(d[s] = 0,s));
28     while(!q.empty()){
29         int u = q.top().second;
30         q.pop();
31         if(done[u]) continue;
32         done[u] = true;
33         for(int i = head[u]; ~i; i = e[i].next){
34             if(d[e[i].to] > d[u] + e[i].w){
35                 d[e[i].to] = d[u] + e[i].w;
36                 q.push(pii(d[e[i].to],e[i].to));
37             }
38         }
39     }
40     return d[t] == INF?-1:d[t];
41 }
42 int main(){
43     int n,s,t,u,v,w,m;
44     while(~scanf("%d%d%d",&n,&s,&t)){
45         memset(head,-1,sizeof head);
46         tot = 0;
47         for(int i = 1; i <= n; ++i){
48             scanf("%d",&m);
49             for(int j = 0; j < m; ++j){
50                 scanf("%d",&v);
51                 add(i,v,j?1:0);
52             }
53         }
54         printf("%d\n",dijkstra(s,t));
55     }
56     return 0;
57 }
View Code