N - Tram - poj1847(简单最短路)
题意:火车从一点开到另一点,轨道上有很多岔路口,每个路口都有好几个方向(火车能够选任意一个方向开),但是 默认的是 第一个指向的方向,所以如果要选择别的方向的话得 进行一次切换操作 ,给定一个起点一个终点 ,问最少进行几次 切换操作 能够 使 火车 完成这个历程 , 如果开不到,输出“-1”。
貌似很简单啊,直接把与第一个相连的距离置为0,后面相连的置为1
然后用最短路的方法直接搞.......最短距离就是开关的次数。
不要忘记还有输出-1;(果断忘了一次)
///////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<vector>
#include<stack>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std; const int maxn = ;
const int maxm = ;
const int oo = 0xfffffff; struct node
{
int u, v, len, next;
}e[maxn]; int dis[maxn], head[maxn];
bool use[maxn]; void Add(int u, int v, int len, int k)
{
e[k].u = u;
e[k].v = v;
e[k].len = len;
e[k].next = head[u];
head[u] = k;
}
void spfa(int s)
{
stack<int> sta;
sta.push(s); while(sta.size())
{
int i = sta.top();sta.pop();
use[i] = false; for(int j=head[i]; j!=; j=e[j].next)
{
int u = e[j].u, v=e[j].v, len = e[j].len; if(dis[u]+len < dis[v])
{
dis[v] = dis[u] + len;
if(use[v] == false)
{
use[v] = true;
sta.push(v);
}
}
}
}
} int main()
{
int N, A, B; while(scanf("%d%d%d", &N, &A, &B) != EOF)
{
int i, j, k=; memset(head, , sizeof(head));
for(i=; i<=N; i++)
{
int M, v;
dis[i] = oo;
scanf("%d%d", &M, &v); Add(i, v, , k++); for(j=; j<M; j++)
{
scanf("%d", &v);
Add(i, v, , k++);
}
} dis[A] = ; spfa(A); if(dis[B] == oo)
printf("-1\n");
else
printf("%d\n", dis[B]);
} return ;
}
N - Tram - poj1847(简单最短路)的更多相关文章
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- hdu2112(HDU Today 简单最短路)
Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD ...
- Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
problem Bessie is out in the field and wants to get back to the barn to get as much sleep as possibl ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- Tram---poj1847(简单最短路)
题目链接:http://poj.org/problem?id=1847 题意:给了N个交叉口,每个交叉口有自己能转到的交叉口. 注意这里:First number in the i-th line, ...
- hdu2722 简单最短路,处理好输入就行
题意: 从左上角走到右下角,有的最短时间,每段路径的长度都是2520,每段上都有自己的限制速度,方向. 思路: 直接写就行了,就是个最短路,权值是2520/限制,输入的时候细心点 ...
- HDU 2544(简单最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2544 /* 使用pair代替结构 */ #include <iostream> #include & ...
- HDU 1874(简单最短路) (大优化)
优先队列那里用greater会报错 http://acm.hdu.edu.cn/showproblem.php?pid=1874 /* 使用pair代替结构 */ #include <iostr ...
- LightOJ 1321 - Sending Packets 简单最短路+期望
http://www.lightoj.com/volume_showproblem.php?problem=1321 题意:每条边都有概率无法经过,但可以重新尝试,现给出成功率,传输次数和传输时间,求 ...
随机推荐
- Linux下inotify监控文件夹状态,发生变化后触发rsync同步
1.安装工具--inotifywget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar ...
- Oracle 10G强大的SQL优化工具:SQL Tuning Advisor
p { margin-bottom: 0.25cm; direction: ltr; color: rgb(0, 0, 0); line-height: 120%; orphans: 2; widow ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- SGU 143.Long Live the Queen(女王万岁)
时间限制:0.25s 空间限制:4M 题意: 有n(n<=16000)个小镇,每两个小镇有且仅有一条路径相连.每个小镇有一个收益x(-1000<=x<=1000). 现在要求,选择一 ...
- LGDT/LIDT-加载全局/中断描述符表寄存器
将源操作数中的值加载到全局描述符表寄存器 (GDTR) 或中断描述符表寄存器 (IDTR).源操作数指定 6 字节内存位置,它包含全局描述符表 (GDT) 或中断描述符表 (IDT) 的基址(线性地址 ...
- ecshop 商店设置,新增或者修改字段
当想要新增选项到ecshop的商店设置时, 第一步:在ecs_shop_config这个表增加一条设置项记录 parent_id.code.type.value type 会有 group text ...
- js定位navigator.geolocation
一.简介 html5为window.navigator提供了geolocation属性,用于获取基于浏览器的当前用户地理位置. window.navigator.geolocation提供了3个方法分 ...
- php函数——『解析 xml数据』
<?php //该文件是 //$raw_post_data = file_get_contents('php://input'); //file_put_contents('a.txt', $r ...
- Python,遍历目录下TXT
import os #获取根目录,递归得到所以txt文件的路径 list_dirs = os.walk(os.curdir) txtfilenames=[] for root, dirs, files ...
- 性能相差极大的SQL语句
等价的SQL,性能差异极大,数据库里设计了一个字段存储日期时间,但不是datetime类型,用了时间戳(int 11), 下面有2个SQL语句用于查询数据库,一个是把时间戳转成date进行查询,一个是 ...