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的更多相关文章

  1. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  2. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  3. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  4. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  5. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  6. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  7. (简单) POJ 1847 Tram,Dijkstra。

    Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...

  8. Floyd_Warshall POJ 1847 Tram

    题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...

  9. POJ 1847 Tram【Floyd】

    题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...

随机推荐

  1. jQuery对select操作小结

    //遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] ...

  2. ExtJs Grid 删除,编辑,查看详细等超链接处理

    在网上查了好多资料,关于ExtJs处理操作栏的“删除”.“编辑”.“查看详细”的处理,原来项目都是这么处理: 操作栏:{ text:'操作', xtype:'actioncolumn', items ...

  3. ListView的LayoutParams设置

    // Temp is the root view that was found in the xml final View temp = createViewFromTag(root, name, a ...

  4. Cordova for Android(Windows)环境配置

    PS:注意事项 一些坑在此声明: 1.安装Eclipse后,记得设置各项编码格式为utf-8 请移步:http://www.blogjava.net/xiaomage234/archive/2014/ ...

  5. 腾讯数据总监:运营人员必须掌握的APP基础数据分析体系(没有比这篇更系统全面的)

    导读:在互联网企业,任何一个APP都要事先规划好数据体系,才允许上线运营,有了数据才可以更好的科学运营.因此本文将为大家介绍APP的基础数据指标体系.主要分为五个维度,包括用户规模与质量.参与度分析. ...

  6. Java通过继承thread类与实现Runnable接口实现多线程的区别

    Java中线程的创建有两种方式: 1.  通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中 2.  通过实现Runnable接口,实例化Thread类 一.通过继承T ...

  7. C# 中的"yield"使用

    yield是C#为了简化遍历操作实现的语法糖,我们知道如果要要某个类型支持遍历就必须要实现系统接口IEnumerable,这个接口后续实现比较繁琐要写一大堆代码才能支持真正的遍历功能.举例说明 usi ...

  8. 正则表达式(http://tieba.baidu.com/p/882391125)

      觉得不错,就记下来了   正则表达式30分钟入门教程版本:v2.31 (2009-4-11) 作者:deerchao 转载请注明来源30分钟内让你明白正则表达式是什么,并对它有一些基本的了解,让你 ...

  9. (4) 深入理解Java Class文件格式(三)

    转载:http://blog.csdn.net/zhangjg_blog/article/details/21557357 首先, 让我们回顾一下关于class文件格式的之前两篇博客的主要内容. 在  ...

  10. C语言面试题(二)

    上篇对嵌入式中C语言基本数据类型,关键字和常用操作进行了汇总,这篇我们将侧重字符串操作.请看下面的字符串处理函数:    a.库函数    1)将字符串src拷贝到字符数组dest内        c ...