Tram POJ - 1847
题目链接:https://vjudge.net/problem/POJ-1847
思路:想从A到B使用开关少,想清楚了就是个简单的最短路,可以把不用开开关为权值0,
要开开关为权值1,就是求A到B开开关最少的次数,题目说了,每行第一个点是第 i-th点和他正好数开关开的方向连接。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std; typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
int head[N];
bool vis[N];
int dis[N];
int cnt;
int n,s,t; struct Edge{
int to;
int w;
int next;
}e[N * N]; struct node{
int pos;
int w; bool friend operator< (const node& a,const node& b){
return a.w > b.w;
}
}; void add(int u,int v,int w){
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
} void dijkstra(){ rep(i,,n) vis[i] = false;
rep(i,,n) dis[i] = inf;
dis[s] = ; priority_queue<node> que;
que.push(node{s,}); int pos,v,w;
while(!que.empty()){
pos = que.top().pos;
que.pop(); if(vis[pos]) continue;
vis[pos] = true; for(int o = head[pos]; ~o; o = e[o].next){
v = e[o].to;
w = e[o].w; if(!vis[v] && dis[pos] + w < dis[v]){
dis[v] = dis[pos] + w; que.push(node{v,dis[v]});
}
}
} if(dis[t] == inf) cout << "-1" << endl;
else cout << dis[t] << endl;
} int main(){ scanf("%d%d%d",&n,&s,&t); rep(i,,n) head[i] = -;
int cnt = ; int tot,v;
rep(u,,n){
cin >> tot; rep(i,,tot){
cin >> v; if(i == ) add(u,v,);
else add(u,v,);
}
}
dijkstra(); getchar(); getchar();
return ;
}
Tram POJ - 1847的更多相关文章
- Day4 - L - Tram POJ - 1847
Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In e ...
- 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 ...
随机推荐
- B1020 月饼(25分)
#include<cstdio> #include<algorithm> #include<iostream> using namespace std; struc ...
- scss 入门基础
在一个项目中,样式是必不可少的一部分,而对于一个完整的项目来说是有个基准色调的.在项目需求变化不大的情况下,可以直接在css中写这些颜色值之类的东西.但是如果遇到一个朝令夕改的领导或者甲方,那会变得相 ...
- python前后台tcp/udp通讯示例
以下代码兼容python2.7+.python3 TCP示例 服务器 -- sever_tcp.py #!/usr/bin/env python #coding=utf-8 import time i ...
- Scala Symbol解释
Symbol This class provides a simple way to get unique objects for equal strings. Since symbols are i ...
- Mac修改hosts方法
总有各种各样的原因需要修改hosts文件,那么就来简介下怎么修改.terminal中打开hosts: sudo vim /private/etc/hosts 打开文件后I开启插入模式,在最后一行添加你 ...
- 1+x证书《Web前端开发》等级考试样题
Web前端开发初级理论考试样题2019 http://blog.zh66.club/index.php/archives/149/ Web前端开发初级实操考试样题2019 http://blog.zh ...
- AppBarLayout折叠时候的阴影
最近在项目中遇到一个需求,AppBarLayout在完全展开时没有阴影,在完全收缩时展示阴影,这个功能可以通过设置StateListAnimator的轻松的实现. 首先,在res/animator目录 ...
- SQL索引管理器 - 用于SQL Server和Azure上的索引维护的免费GUI工具
我作为SQL Server DBA工作了8年多,管理和优化服务器的性能.在我的空闲时间,我想为宇宙和我的同事做一些有用的事情.这就是我们最终为SQL Server和Azure 提供免费索引维护工具的方 ...
- c# 编码风格
此内容为copy别人的,仅供自己参看.如有意见,麻烦通知我,谢谢 1. C# 代码风格要求 1.1注释 类型.属性.事件.方法.方法参数,根据需要添加注释. 如果类型.属性.事件.方法.方法参数的名称 ...
- 开发--Deepin系统安装
开发|Deepin系统安装 在18小时前,我刚刚萌生了一个将我的笔记本换成linux系统.在18小时后的现在,在我各种试错之后,笔记本已经开始跑起linux了.在科技的时代,只要是想法,都可以试一试. ...