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: 184764-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.
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.
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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
相关文章
- poj 3262 Protecting the Flowers 贪心 牛吃花
- poj 3630 Phone List
- Java实现 POJ 2749 分解因数(计蒜客)
- POJ 1936 All in All(模拟)
- poj 2559求柱形图中最大矩形
- poj(1011)——Sticks(经典的dfs+剪枝)
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
- poj 1664 把平果
- POJ 3253 Fence Repair(哈夫曼树)
- POJ - 1422 Air Raid 二分图最大匹配
- Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)