Day4 - L - Tram POJ - 1847
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
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
Sample Input
3 2 1
2 2 3
2 3 1
2 1 2
Sample Output
0
简述:题有点难懂,给你N组数以及起点A终点B,节点标号1-N,接下来每一行第一个数表示i-th连接有几个节点,后面的第一个数是默认方向不用改变,后续的都是需要改变一次方向。
思路:看懂题意后就是一个最短路问题,默认方向权为0,改变为1,四种算法选一种即可,我这里用的是dijkstra,(其他三种在A题中有,这里就不写了),代码如下:
const int maxm = ;
const int INF = 0x7ffffff; int N, A, B, d[maxm], vis[maxm]; struct Edge {
int from, to, dist;
Edge(int _from, int _to, int _dist) : from(_from), to(_to), dist(_dist){};
}; struct Node {
int from, dist;
Node(int _from, int _dist) : from(_from), dist(_dist){}
bool operator<(const Node &a)const {
return a.dist < dist;
}
}; vector<Edge> edges;
vector<int> G[maxm]; void addedge(int u, int v, int dist) {
edges.push_back(Edge(u, v, dist));
G[u].push_back(edges.size() - );
} void init() {
for(int i = ; i <= N; ++i) {
d[i] = INF;
G[i].clear();
}
edges.clear();
memset(vis, , sizeof(vis));
} int main() {
while(scanf("%d%d%d", &N, &A, &B) != EOF) {
init();
for (int i = ; i <= N; ++i) {
int t1, t2;
scanf("%d", &t1);
for(int j = ; j < t1; ++j) {
scanf("%d", &t2);
addedge(i, t2, j == ? : );
}
}
priority_queue<Node> q;
q.push(Node(A, ));
d[A] = ;
while(!q.empty()) {
Node p = q.top();
q.pop();
if(vis[p.from])
continue;
vis[p.from] = ;
int len = G[p.from].size();
for(int i = ; i < len; ++i) {
if(d[edges[G[p.from][i]].to] > d[p.from] + edges[G[p.from][i]].dist) {
d[edges[G[p.from][i]].to] = d[p.from] + edges[G[p.from][i]].dist;
q.push(Node(edges[G[p.from][i]].to, d[edges[G[p.from][i]].to]));
}
}
}
printf("%d\n", d[B] >= INF?-:d[B]);
}
return ;
}
Day4 - L - Tram POJ - 1847的更多相关文章
- Tram POJ - 1847
题目链接:https://vjudge.net/problem/POJ-1847 思路:想从A到B使用开关少,想清楚了就是个简单的最短路,可以把不用开开关为权值0, 要开开关为权值1,就是求A到B开开 ...
- Tram POJ - 1847 spfa
#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...
- POJ 1847 Tram (最短路径)
POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...
- 最短路 || POJ 1847 Tram
POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...
- poj 1847 最短路简单题,dijkstra
1.poj 1847 Tram 最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...
- poj 1847 Tram
http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...
- [最短路径SPFA] POJ 1847 Tram
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...
- 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 ...
随机推荐
- 了解 C++
C++的历史 C++由C语言发展演变而来,最初被称为"带类的C" 1983年正式取名为C++ 1998年11月被国籍标准化组织(ISO)批准为国际标准 2003年10月15日发布了 ...
- OA:办公自动化———笔记一
oa:办公自动化 1.对公司结构的管理 基础数据管理 部门进行管理 角色进行管理 权限进行管理 员工进行管理 2.流程管理 利用工作流技术对比较 ...
- idea设置自带的maven为国内镜像
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/panchang199266/articl ...
- 穿越雷区--蓝桥杯--DFS/BFS
题目描述 X星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废. 某坦克需要从A区到B区去(A,B区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短? ...
- linux 部署java 项目命令
1:服务器部署路径:/home/tomcat/tomcat/webapps (用FTP工具链接服务器把包上传到此目录) 2:进入项目文件夹 cd /home/tomcat/tomcat/webapp ...
- 「Luogu P3680 凸轮廓线」
一道神奇的计算几何题 前置芝士 正三角形,正方形,圆:什么,您都会,那真是太好了. 三角函数的运用:因为我不是很想在这一块写太多,具体可以自行百度. 推导公式 对于一串是圆和正方形开头和结尾时是十分好 ...
- 111、Java中String类之字符串文本全部拆分
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 网易云信-新增自定义消息(iOS版)
https://www.jianshu.com/p/2bfb1c4e9f21 前言 公司业务需要,PC端,移动端都用到了第三方 网易云信 IM来实现在线客服咨询.在这当中难免遇到一些需求是网易云信没有 ...
- 吴裕雄--天生自然HADOOP操作实验学习笔记:安装zookeeper集群
实验目的 了解zookeeper的概念和原理 学会安装zookeeper集群并验证 掌握zookeeper命令使用 实验原理 1.Zookeeper介绍 ZooKeeper是一个分布式的,开放源码的分 ...
- 守神漏洞扫描器V1.2
主界面 指纹利用 漏洞库 怎么说呢,个人感觉这个扫描器跟小哲的Test404Fuzzer差不多~ 就是功能多了旁站查询.C段查询.而且这款工具的exp比Test404Fuzzer的多了几个~ 总体来说 ...