[最短路径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 ...
随机推荐
- javascript设计模式简介
- HtmlHelper的扩展
HtmlHelper的扩展: 注意点:扩展方法必须是静态方法,所在的类必须是静态类,所在的命名空间改成System.Web.MVC则能省略页面中必须添加命名空间的约束. //主要就是输出分页的超级链接 ...
- Upload file
<h3>Upload File</h3> <form action="@Url.Action("Upload","UploadCo ...
- __block 与 __weak的区别理解
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
- T-SQL 的简单查询语句
通配符: “_”: 代表匹配一个字符 “%”: 代表匹配多个字符: []:表示范围,可以包含多个数据 [^] 表示取反 “-“ 表示范围 逻辑与 and 逻辑或 or 逻辑非 not 聚会函数 : ...
- hdu 5779 Tower Defence
题意:考虑由$n$个结点构成的无向图,每条边的长度均为$1$,问有多少种构图方法使得结点$1$与任意其它节点之间的最短距离均不等于$k$(无法到达时距离等于无穷大),输出答案对$1e9+7$取模.$1 ...
- ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多
ListView实现Item上下拖动交换位置 并且实现下拉刷新 上拉加载更多 package com.example.ListViewDragItem; import android.app.Ac ...
- GZFramwork快速开发框架演练之会员系统(四)添加商品管理
1.1:创建表结构 新建三张商品关联的表,表模型如下: 创建SQL语句略 1.2:生成表Model(生成方法见上一节) 1.3:生成tb_ProductType的单结构界面然后添加到项目中 1.4:修 ...
- MVC区域使用
新建项目 Main: 添加一个MVC5控制器并添加index视图:(HomeController) Views/Home/Index.cshtml内容: @{ Layout = null; } < ...
- PHP-----循环结构
for循环语句 打印金字塔 完整的金字塔 //打印金字塔 $n=25; for($i=1;$i<=$n;$i++){ //空格循环 for($k=1;$k<=$n-$i;$k++){ ec ...