POJ 1847 Tram (最短路)
Tram
题目链接:
http://acm.hust.edu.cn/vjudge/contest/122685#problem/N
Description
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
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
Hint
##题意:
求图中#A到#B的最短路.
##题解:
裸的最短路,数据非常小,随便求就好.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 1000000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;
int n,s,t;
int dis[maxn][maxn];
void init() {
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j] = (i==j? 0:inf);
}
void floyd() {
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(dis[i][j] > dis[i][k]+dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
int main(void)
{
//IN;
while(scanf("%d %d %d", &n,&s,&t) != EOF)
{
init();
for(int i=1; i<=n; i++) {
int m; scanf("%d", &m);
for(int j=1; j<=m; j++) {
int x; scanf("%d", &x);
dis[i][x] = j==1? 0:1;
}
}
floyd();
if(dis[s][t] == inf) printf("-1\n");
else printf("%d\n", dis[s][t]);
}
return 0;
}
POJ 1847 Tram (最短路)的更多相关文章
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
随机推荐
- linux下文件夹的创建、复制、剪切、重命名、清空和删除命令
在home目录下有wwwroot目录,wwwroot下有sinozzz目录,即/home/wwwroot/sinozzz 一.目录创建 在/home/wwwroot目录下新建一个sinozzz123的 ...
- poj 3792 Area of Polycubes (简单模拟)
题目 题意:在三维坐标系中,给定n个立方体的中心坐标,立方体的边长为1,按照输入顺序,后来输入的必须和之前输入的立方体有公共的边. 而且,不能和之前输入的立方体相同. 如果满足条件,输出表面积.如果不 ...
- UVA 1663 Purifying Machine (二分图匹配,最大流)
题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- Java [Leetcode 206]Reverse Linked List
题目描述: Reverse a singly linked list. 解题思路: 使用递归或者迭代的方法. 代码如下: 方法一:递归 /** * Definition for singly-link ...
- hdu 4435 charge-station
// 题意 从1出发逛完N个点回到出发点 要在这N个点选择性建设加油站 车每次加满油最多可以行使D米// 然后最少要花多少钱才能达到上述要求// 注意到 第i个城市的花费是 2^(i-1) 所以 我就 ...
- 深入学习Oracle分区表及分区索引
关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: • Range(范围)分区 • Has ...
- Android-setDefaultKeyMode方法
在Activity中的onCreate()方法中使用setDefaultKeyMode()可以做到在当前activity中打开拨号器.执行快捷键.启动本地搜索.启动全局搜索: setDefaultKe ...
- 最好最实用的PHP二次开发教程
◆二次开发 1.什么是二次开发? 二次开发,简单的说就是在现有的软件上进行定制修改,功能的扩展,然后达到自己想要的功能和效果,一般来说都不会改变原有系统的内核. 2.为什么要二次开发? 随着信息化技术 ...
- HDU 5875 Function
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...