Floyd_Warshall POJ 1847 Tram
题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换.
分析:把第一个城市权值设为0, 其余设为0.然后Floyd跑一下,得到A到B最少转换几次.有点水
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int N = 1e2 + 5;
const int INF = 0x3f3f3f3f;
int d[N][N];
bool vis[N];
int n, m; void Floyd_Warshall(void) {
for (int k=1; k<=n; ++k) {
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
d[i][j] = min (d[i][j], d[i][k] + d[k][j]);
}
}
}
} int main(void) {
int A, B;
while (scanf ("%d%d%d", &n, &A, &B) == 3) {
memset (d, INF, sizeof (d));
for (int i=1; i<=n; ++i) {
int c; scanf ("%d", &c);
for (int v, j=1; j<=c; ++j) {
scanf ("%d", &v);
if (j == 1) d[i][v] = 0;
else d[i][v] = 1;
}
}
Floyd_Warshall ();
int ans = d[A][B];
if (ans == INF) ans = -1;
printf ("%d\n", ans);
} return 0;
}
Floyd_Warshall 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
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
随机推荐
- idea 用maven骨架生成项目速度慢的问题
使用mvn archetype:generate命令时,加上-DarchetypeCatalog=local archetypeCatalog=local
- maven配置httpclient3.X jar包
<dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging& ...
- NYOJ题目769乘数密码
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsQAAAJYCAIAAADqk2fsAAAgAElEQVR4nO3dPVLrytbG8XcS5AyEWA
- php连接sql server
这两天有个php连接sql server的项目,顺便学习学习sql server 说明: 1:PHP5.2.x本身有个php_mssql.dll的扩展用来连接Sql server,但是这个dll只是 ...
- Android Programming: Pushing the Limits -- Chapter 1: Fine-Tuning Your Development Environment
ADB命令 Application Exerciser Monkey Gradle ProGuard 代码重用 版本控制 静态代码分析 代码重构 开发者模式 ADB命令: @.adb help:查 ...
- svn不知道这样的主机
重做服务器后,计算机名称肯定是不一样的.我们之前的项目还是老计算机名字,只要更改一下计算机名字即可实现.或者更改
- 重温WCF之发送和接收SOAP头(三)
SOAP头可以理解为一种附加信息,就是附加到消息正文的内容. 既然消息头是附加信息,那有啥用呢?你可别说,有时候还真有不少用处.举个例子,WCF的身份验证是不是很麻烦?还要颁发什么证书的(当然不是荣誉 ...
- Jmeter在restful风格接口测试中的应用
1.如何下载安装 官网下载,一个压缩包apache-jmeter-3.0.zip,解压即可,打开bin目录下jmeter.bat即可打开软件. 2.熟悉界面 3.实际案例 测试restful风格接口 ...
- [LeetCode] Remove Element (三种解法)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 算法系列:XXX
转载自http://www.cnblogs.com/skynet/p/3372855.html 这次分享的宗旨是——让大家学会创建与使用静态库.动态库,知道静态库与动态库的区别,知道使用的时候如何选择 ...