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 ...
随机推荐
- Anchor和Dock的区别
Dock的Bottom,整个控件填充下半部分,控件会被横向拉长 Anchor,仅仅是控件固定在下方,位置不会发生移动,自动锚定了此控件和父容器的底部的间隔 Anchor可以确定控件的相对位置不发生变化
- Android开发之assets文件夹中资源的获取
assets中的文件都是保持原始的文件格式,需要使用AssetManager以字节流的形式读取出来 步骤: 1. 先在Activity里面调用getAssets() 来获取AssetManager引用 ...
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- 函数buf_page_hash_get_low
/******************************************************************//** Returns the control block of ...
- Self-Paced Training (3) - Docker Operations
AgendaTroubleshooting ContainersOverview of Security PracticesPrivate RegistryIntro to Docker Machin ...
- 使用讯飞SDK,实现文字在线合成语音
private SpeechSynthesizer mTts; private int isSpeaking = 0; mTts= SpeechSynthesizer.createSynthesize ...
- codeforces 333A - Secrets
题意:保证不能正好配齐n,要求输出可以用的最大硬币数. 注意如果用到某种硬币,那么这种硬币就有无穷多个.所以11=3+3+3+3,12=9+9,13=3+3+3+3+3 #include<cst ...
- Ecshop文件结构,二次开发
文件结构,二次开发有用 ECShop 2.6.2 的结构图及各文件相应功能介绍 ECShop2.6.2 upload 的目录 ┣ activity.php 优惠活动列表 ┣ affiche.php 广 ...
- HDU 5119 Happy Matt Friends
Happy Matt Friends Time Limit: 6000/6000 MS (Java/Others) Memory Limit: 510000/510000 K (Java/Others ...
- c# datagridview与DataSet绑定, 列与数据库表里面的列一一对应
参考代码1: 自己模拟出数据,并分别对dataGridView赋值. using System; using System.Collections.Generic; using System.Comp ...