最短路 || POJ 1847 Tram
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
#define SZ 10005//开n^2的数组!!
#define INF 1e9+10
int head[SZ], nxt[SZ], tot = ;
struct edge
{
int t, d;
}l[SZ];
void build(int f, int t, int d)
{
l[++tot] = (edge){t, d};
nxt[tot] = head[f];
head[f] = tot;
}
int dist[SZ];
bool use[SZ];
queue<int> q;
int spfa(int s, int e)
{
for(int i = ; i < SZ; i++) dist[i] = INF, use[i] = ;
while(!q.empty()) q.pop();
use[s] = ;
dist[s] = ;
q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
use[u] = ;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(dist[v] > dist[u] + l[i].d)
{
dist[v] = dist[u] + l[i].d;
if(!use[v])
use[v] = , q.push(v);
}
}
}
return dist[e];
}
int main()
{
int n, a, b;
scanf("%d %d %d", &n, &a, &b);
for(int i = ; i <= n; i++)
{
int k, x;
scanf("%d", &k);
if(k == ) continue;//考虑k==0的情况
scanf("%d", &x);
build(i, x, );
for(int j = ; j < k; j++)
{
scanf("%d", &x);
build(i, x, );
}
}
int dis = spfa(a, b);
if(dis == INF) printf("-1\n");
else printf("%d\n", dis);
return ;
}
最短路 || 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 (最短路)
Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...
- 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,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- POJ 1847 Tram【Floyd】
题意:给出n个站点,每个站点都有铁路通向其他站点 如果当前要走得路恰好是该站点的开关指向的铁路,则不用扳开关,否则要手动扳动开关,给出起点和终点,问最少需要扳动多少次开关 输入的第一行是n,start ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
随机推荐
- Ruby 类的创建
class Language def initialize(name, creator) @name = name @creator = creator end def description pu ...
- View Controller Programming Guide for iOS---(一)---About View Controllers
About View Controllers View controllers are a vital link between an app’s data and its visual appear ...
- E20180410-hm
preface n. 序言,引语; 开端,前奏; [宗] (弥撒的) 序诵,序祷; vi. 作序; 作为…的序言,作为…的开端; 给…作序; 开始,导致; continue vi. 持 ...
- 洛谷 - P2762 - 太空飞行计划问题 - 最小割
https://www.luogu.org/problemnew/solution/P2762 最小割对应的点,在最后一次更新中dinic的bfs会把他的dep重置掉.所以可以根据这个性质复原最小割. ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- HDOJ1584蜘蛛牌【DFS】
10张牌,大的只能跟小的跑,可以针对每一个状态进行搜索,求一个最小的移动距离. 但是不会怎么遍历整个状态是硬伤? 因为只能大的跟着小的. 先把小的标记,去寻找大的点,最终一定是满足的吧. 比如先标记1 ...
- IT兄弟连 JavaWeb教程 请求转发案例
CheckServlet类与OutputServlet类之间为请求转发关系.在web.xml文件中,为CheckServlet映射的URL为"/check",为OutputServ ...
- Integer Cache(带你脱坑)
Integer Cache 废话不多说----->直接上代码: public class IntegerDemo { public static void main(String[] args) ...
- jQuery笔记之工具方法
jQuery 工具方法 $.type()判断数据类型 $.isArray() $.isFunction() $.isWindow()... $.trim()消除空格 $.proxy()改变this指向 ...
- Win7执行应用报CLR20r3错误处理记录
Windows7环境下运行应用报"CLR20r3"错误,错误信息如下: 问题详细信息: 问题签名: 问题事件名称: CLR20r3 问题签名 : qbbtools.exe 问题签名 ...