[最短路径SPFA] POJ 1847 Tram
Tram
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 14630 Accepted: 5397
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 <= 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.
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
Source Croatia OI 2002 Regional - Juniors
原题大意:输入三个数N,A,B;N代表有多少个点,A代表起始点,B代表终点。
接下来的N行,第i行第一个数T代表在i点的边的个数,接下来读入T个数代表与它相连的点,除了第一个数与i点的路径长为0,其他全为1.
求最短路径。
解题思路:裸一遍SPFA,FLOYYED或者迪杰斯特拉都可以解决这道题。
以下是SPFA的解法,已经注释。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
queue<int> q;
struct mp
{
int len,next,to;
}map[12010];
int num,frist[12010];
void add(int x,int y,int len)
{
++num;
map[num].to=y;map[num].len=len;
map[num].next=frist[x];frist[x]=num; //用数组模拟链表,存储边
}
void spfa(int begin,int end)
{
bool visit[12010];
int x,p,i,dist[12010];
while(!q.empty()) q.pop();
memset(visit,false,sizeof(visit));
for(i=0;i<12010;++i) dist[i]=inf; //dist[I]代表I点距离begin的距离
q.push(begin);dist[begin]=0;
while(!q.empty())
{
x=q.front();visit[x]=false;
for(i=frist[x];i;i=map[i].next) //枚举与x点相连的每一个边
{
if(dist[x]+map[i].len<dist[map[i].to])
{
dist[map[i].to]=dist[x]+map[i].len;
if(!visit[map[i].to])
{
q.push(map[i].to);
visit[map[i].to]=true;
}
}
}
q.pop();
}
if(dist[end]==inf) printf("-1\n");else printf("%d\n",dist[end]);
return;
}
int main()
{
int N,A,B,i,n,x,j,cnt,ans;
while(~scanf("%d%d%d",&N,&A,&B))
{
memset(frist,0,sizeof(frist));
num=0;
for(j=1;j<=N;++j)
{
scanf("%d",&n);
for(i=0;i<n;++i)
{
scanf("%d",&x);
if(i==0) add(j,x,0); else add(j,x,1); //初始化
}
}
spfa(A,B);
}
return 0;
}
[最短路径SPFA] 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 (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
随机推荐
- MVC5关联表读取相关表数据
SchoolName = db.Sys_Company.Find(gr.SchoolCode).FullName 只需Model中指定好SchoolCode是Sys_Company的主键就行了!
- Magento开发常用方法
这里是我做Magento开发常用到的方法,现在总结出来,后续会把更多有用的方法总结出来. 1.直接操作数据库 查找数据:$read = Mage::getSingleton("core/re ...
- 什么是REST、RESTful
1.REST 指的是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是 RESTful. 2.REST 原则是分层系统,这表示组件无法了解它与之交互的中间层以外的组件.通过将系统知识限 ...
- 鸟哥的linux私房菜学习记录之系统设定工具与硬件检测
这部分没啥用,就不记录了,关于服务器的在服务器篇
- SSIS Error The Execute method on the task returned error code 0x80131621
Error Message: The Execute method on the task returned error code 0x80131621 (Mixed mode assembly is ...
- OBD K线抓包 III
14230 HL激活, 5BPS又称 00 //电平激活 C1 33 F1 81 66 //14230的Enter命令 83 F1 11 C1 EF 8F C4 //回应了,一个命令就回应了... ...
- win10查看连接过的wifi密码
cmd窗口 运行 “netsh wlan show profiles name="linasd" key=clear”
- caffe问题集锦
不断更新中... 1.问题:check failure stack trace:*** 解决方法:路径错误,重新去看自己的路径是否有错误 2.Check failed: error == cudaSu ...
- SQL获取第一次出现指定字符前的内容
update Food set FoodTitle=cast(SUBSTRING(FoodTitle,0,PATINDEX('%的%',FoodTitle)) as nvarchar),FoodCod ...
- .sh脚本判断判断某一变量是否为某一数值
.sh脚本中,判断某一变量(例如:OEM_CUSTOMER_SUPPORT)是否为某一数值(例如:0),并根据条件做不同处理,写法如下: if [ $OEM_CUSTOMER_SUPPORT -eq ...