POJ 1847 火车变轨道 (简单最短路)
来源:互联网 发布:手机延时软件 编辑:程序博客网 时间:2024/06/06 13:22
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车 完成这个历程 , 如果开不到,输出“-1”。
貌似很简单啊,直接把与第一个相连的距离置为0,后面相连的置为1
然后用最短路的方法直接搞.......最短距离就是开关的次数。
不要忘记还有输出-1;(果断忘了一次)
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.
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.
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".
3 2 12 2 32 3 12 1 2
0
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<vector>using namespace std;const int N = 111;const int inf = 0x3f3f3f3f;struct P{ int v,c; P(int v,int c):v(v),c(c){} P(){} bool operator < (const P &t1) const { return c>t1.c; }};priority_queue<P>que;vector<P>vec[N];int start,end,n;int dp[N];void dij(){ int i,j; int u,v,c; fill(dp,dp+1+n,inf); dp[start]=0; que.push(P(start,0)); while(!que.empty()) { P p = que.top(); que.pop(); u = p.v; if(dp[u]<p.c) continue; for(i=0;i<vec[u].size();i++) { p = vec[u][i]; v = p.v; if(dp[v]>dp[u]+p.c) { dp[v] = dp[u] + p.c; que.push(P(v,dp[v])); } } } return;}int main(){ int i,j,k,m; int u,v,c,ans; scanf("%d%d%d",&n,&start,&end); for(i=1;i<=n;i++) { scanf("%d",&m); for(j=1;j<=m;j++) { scanf("%d",&v); if(j==1) vec[i].push_back(P(v,0)); else vec[i].push_back(P(v,1)); } } dij(); if(dp[end]==inf) dp[end]=-1; printf("%d\n",dp[end]); return 0;}
0 0
- POJ 1847 火车变轨道 (简单最短路)
- (POJ 1847)Tram 简单最短路 spfa
- POJ 1847 Tram 简单最短路★
- poj 1847最短路
- POJ 1847 Tram(floyd 最短路)
- poj 1847 Tram (最短路)
- POJ 1847 Tram(最短路)
- POJ - 1797 最短路思想加以变化解此题..
- poj 1797 Heavy Transportation 【最短路Dijkstra 变式】
- POJ 2253 最短路的简单变形
- POJ 1847 Tram 最短路
- poj-1847 之最短路
- poj 1847最短路flody
- POJ 1847 Tram(最短路)
- POJ - 1847Tram最短路
- poj 1847 最短路 floyd
- poj-1847 Tram 最短路
- POJ-----1847最短路vector
- EventBus学习笔记(三)
- LaTeX初学知识点总结
- L1-3. 情人节
- Android热修复技术初览
- eclipse下配置tomcat
- POJ 1847 火车变轨道 (简单最短路)
- 算法第五周解题报告
- TensorFlow使用的一个小案例
- Android studio下使用 NDK 之 Hello world
- 利用Python的paramiko+scpclient实现高效和ssh服务器交互
- L1-5. A除以B
- Spring中的bean,factoryBean,beanFactory
- Problem E Red Rover kmp
- 一道据说是Google的面试题:25匹马的角逐