最短路 || 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 ...
随机推荐
- asp.net MVC 单选按钮的使用
单选按钮的标准的html 语法 <form><input type="radio" name="sex" value="male&q ...
- Hadoop 三大调度器源码分析及编写自己的调度器
如要转载,请注上作者和出处. 由于能力有限,如有错误,请大家指正. 须知: 我们下载的是hadoop-2.7.3-src 源码. 这个版本默认调度器是Capacity调度器. 在2.0.2-alph ...
- J20170422-hm
ワイルドスクリプト wild script 通配符 シェルスクリプト shell脚本
- codforces 1C Ancient Berland Circus(几何)
题意 给出正多边形上三个点的坐标,求正多边形的最小面积 分析 先用三边长求出外接圆半径(海伦公式),再求出三边长对应的角度,再求出三个角度的gcd,最后答案即为\(S*2π/gcd\),S为gcd对应 ...
- PTA【复数相乘】
队友在比赛时A掉的.吊吊吊!!! 主要考虑这些情况吧||| 案例: /* 3i i -3 i -1-i 1+i i 1 -1-i -1-i */ -3 -3i -2i i 2i #include &l ...
- Python之单元测试——HTMLTestRunner
前置条件:把HTMLTestRunner.py文件拷贝到External Libraries—>site-packages里面 import unittestimport HTMLTestRun ...
- Hdu 2888 Check Corners (二维RMQ (ST))
题目链接: Hdu 2888 Check Corners 题目描述: 给出一个n*m的矩阵,问以(r1,c1)为左上角,(r2,c2)为右下角的子矩阵中最大的元素值是否为子矩阵的顶点? 解题思路: 二 ...
- 为页面添加favicon
<link rel="shortcut icon" href="favicon.ico" /> 还有另一种写法,但是IE对它的支持不够好: < ...
- Python 3.6.5安装过程中小错误zipimport.ZipImportError: can't decompress data; zlib not available
执行 :yum install -y zlib*之后,就好了.该安装错误是在CentOS7.4中遇到的.
- DNS正、反向解析+负载均衡+智能DNS+密钥认证
主机名 IP 软件包 系统版本 内核版本 实验环境 master 192.168.30.130 bind.x86_64 32:9.8.2-0.17.rc1.el6_4.6 bind-chroot.x8 ...