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 ...
随机推荐
- C#文件夹和文件操作
File.Exist(string path)//文件读写FileStream fs=new FileStream(filename, FileMode.Create);BinaryWriter bw ...
- osgearth使用prjected投影
In projected mode, you have to specify a map profile (i.e. a map projection). You also need to tell ...
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...
- UICollectionView cellForItemAtIndexPath 方法不走
在storyboard 中 UICollectionView cellForItemAtIndexPath not called 被坑了好久,各种问题点查找,终于解决了 解决办法: self.auto ...
- mongodb3.x用户角色
用户和角色是多对多的关系,一个用户可以对应多个角色,一个角色可以拥有多个用户.用户角色的不同对应的权限也是不一样的.下面是一些分配给用户的常见的角色. read ...
- IIS6与IIS7中如何设置文件过期
在IIS6中:一. 打开IIS管理器 二. 选中要设置的网站单击属性,打开站点属性菜单 三. 单击HTTP头选项卡 四. 单击 启用内容过期 如:设置30分钟后过期,此时间段后过期项中填30,单位选择 ...
- Gson 解析列表
JsonArray jsonArray = new JsonParser().parse(resp).getAsJsonObject().getAsJsonArray("list" ...
- 【JAVA基本数据类型包装类】
一.概述 JAVA中一共有8种数据类型,分别是byte short int long boolean float double char,与此相对应的,有8个类与它们分别对应: byte Byte ...
- SQLAlchemy ORM之建表与查询
作了最基本的操作,找找感觉.. #coding=utf-8 from datetime import datetime from sqlalchemy import (MetaData, Table, ...
- CXF学习(3) wsdl文件
<!--一次webservice调用,其实并不是方法调用,而是发送SOAP消息 ,即xml片段--> <!--以上一篇中的wsdl文档为例,这里我将注释写到文档中 --> &l ...