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. Windows Azure Table storage 之 动态Table类 DynamicTableEntity

    在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示. public class CustomerEntity : ...

  2. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  3. 初识python第一天

    一.python简介 1.1 python的诞生 python的创始人吉多.范罗苏姆(Guido van Rossum),他在开发python语言之前曾使用过几年的ABC语言,ABC是一门主要用于教学 ...

  4. linux deepin-scrot 截图工具

    1.下载 .deb 安装包: 点击这里   (如果提示缺少依赖,去终端安装相应的依赖) 2. 设置快捷键Alt+Ctrl+A 1. 系统设置 -> 键盘设置 -> 自定义快捷键 -> ...

  5. SQL SERVER建库&用户赋权限

    create database ServiceDB on primary ( name='ServiceDB_data', -- 主数据文件的逻辑名称 filename='D:\WebRoot\DB\ ...

  6. js函数、变量提升(hoisting)

    其实我只是想复习下变量提升的,然后看到了函数提升,然后再看到了函数声明.函数表达式. 有必要怀着敬仰之心提及园子里的TOM大叔的解密命名函数表达式,不愧是大叔,好好地脑补了下基础知识. 在ECMASc ...

  7. Page Visibility API(页面可见性)

    页面可见性: 就是对于用户来说,页面是显示还是隐藏, 所谓显示的页面,就是我们正在看的页面:隐藏的页面,就是我们没有看的页面. 因为,我们一次可以打开好多标签页面来回切换着,始终只有一个页面在我们眼前 ...

  8. hdu3847Trash Removal(凸包)

    链接 这题居然是WF的题, 应属于签到题.. 求一个多边形是否能被一个宽为d的矩形框住. 可以求一下凸包,然后枚举每条凸包的边,找出距离最远的点. #include <iostream> ...

  9. 点击datagrid弹出ldhdialog,点击弹出框的按钮,关闭且刷新datagrid

    datagrid里的js这么写 //点击添加按钮触发 function superadd(title,addurl,gname,width,height) { gridname=gname; crea ...

  10. aws ftp

    amazon ec2 运行后,可以用key pair ssh到终端, 不得不承认key pair是很安全的一种方式, 但是安全意味着麻烦,要登陆ssh必须随身带着key pair, 不过个人见意还是不 ...