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. 简单的bat命令去获取wsdl的源码

    @echo off cd C:\Program Files\Java\jdk1..0_45\bin for /R "D:\wqcCode\company\emtproj\02 Require ...

  2. jquery判断起止时间大小和非空

    //时间判断 function CheckDate() { var startTime = $('#txtTime').val(); //获取当前日期 var start = new Date(sta ...

  3. java 将list 按长度分割

    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {      ...

  4. noi 2989 糖果

    题目链接:http://noi.openjudge.cn/ch0206/2989/ 首先,数据很大,直接用背包会re. 这里增加的是对%k 的余数维度.f[i][j] 表示前 i 种糖果取到总颗数模 ...

  5. ambari之hbase数据迁移

    一.hbase原理剖析 Base是一个构建在HDFS上的分布式列存储系统:HBase是基于Google BigTable模型开发的,典型的key/value系统:HBase是Apache Hadoop ...

  6. [问题2015S05] 复旦高等代数 II(14级)每周一题(第六教学周)

    [问题2015S05]  设 \(A\) 是 \(n\) 阶复方阵, 证明: \(A\) 可对角化的充分必要条件是 \(A\) 相似于某个如下的循环矩阵: \[C=\begin{pmatrix} a_ ...

  7. ArcgisAdd-In开发入门实例

    1.开发环境 Vs2012+Arcgis10.2+win7 64bit 2.实现代码 首先在VS2012中新建一个解决方案,命名AddInTest. 接着,给解决方案AddInTest新建一个项目: ...

  8. 《BI那点儿事》数据流转换——派生列

    派生列转换通过对转换输入列应用表达式来创建新列值. 表达式可以包含来自转换输入的变量.函数.运算符和列的任意组合. 结果可作为新列添加,也可作为替换值插入到现有列. 派生列转换可定义多个派生列,任何变 ...

  9. python打怪之路【第三篇】:利用Python实现三级菜单

    程序: 利用Python实现三级菜单 要求: 打印省.市.县三级菜单 可返回上一级 可随时退出程序 coding: menu = { '北京':{ '朝阳':{ '国贸':{ 'CICC':{}, ' ...

  10. Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

    #!/usr/bin/Python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的 估计有不少人注意过一些python脚本开头 ...