POJ 1847 Tram dij
分析:d[i]表示到i点,最少的操作数
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=+;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w,next;
bool operator<(const Edge &e)const{
return w>e.w;
}
}edge[N*N];
int head[N],tot,n,s,t,d[N];
void add(int u,int v,int w){
edge[tot].v=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
priority_queue<Edge>q;
bool vis[N];
int dij(int s,int t){
for(int i=;i<=n;++i)d[i]=INF,vis[i]=;
d[s]=,q.push(Edge{s,,});
while(!q.empty()){
while(!q.empty()&&vis[q.top().v])q.pop();
if(q.empty())break;
int u=q.top().v;
q.pop();
vis[u]=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]&&d[v]>d[u]+edge[i].w){
d[v]=d[u]+edge[i].w;
q.push(Edge{v,d[v],});
}
}
}
return d[t]==INF?-:d[t];
}
int main(){
scanf("%d%d%d",&n,&s,&t);
memset(head,-,sizeof(head));
for(int i=;i<=n;++i){
int k,v;
scanf("%d",&k);
for(int j=;j<=k;++j){
scanf("%d",&v);
add(i,v,j==?:);
}
}
printf("%d\n",dij(s,t));
return ;
}
POJ 1847 Tram dij的更多相关文章
- 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 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 ...
- (简单) POJ 1847 Tram,Dijkstra。
Description Tram network in Zagreb consists of a number of intersections and rails connecting some o ...
- POJ - 1847 Tram(dijkstra)
题意:有向图有N个点,当电车进入交叉口(某点)时,它只能在开关指向的方向离开. 如果驾驶员想要采取其他方式,他/她必须手动更换开关.当驾驶员从路口A驶向路口B时,他/她尝试选择将他/她不得不手动更换开 ...
- Floyd_Warshall POJ 1847 Tram
题目传送门 题意:这题题目难懂.问题是A到B最少要转换几次城市.告诉每个城市相连的关系图,默认与第一个之间相连,就是不用转换,其余都要转换. 分析:把第一个城市权值设为0, 其余设为0.然后Floyd ...
随机推荐
- 关于php的认识和介绍
php的介绍: 什么是php? <1>:php是一个编程语言 <2>:php是处理php编程语言的一个软件.php语言必须运行在php软件上. 为什么要学习php? php可以 ...
- php使用phpmailer发送邮件
本人新手,由于要做邮件发送验证码,所以找到和搜集到这些,本人亲测完全可以用 这是163邮箱的 因为不是企业邮箱填写的账号是163的账号,但是密码是授权码 授权码的获取方式为:
- 制作Mac OS X Mavericks 安装U盘
1. 8G+ U盘一个. 2. App Store 下载Maverics到本地(默认会下载到Applications) 2. 打开Mac OS 磁盘工具(Disk Utility),左侧选中U盘,在右 ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- cocos2dx3.4 解析json文件
头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...
- ios7 sdk 新特性
iOS 7 is a major update with compelling features for developers to incorporate into their apps. The ...
- Python属性、方法和类管理系列之----__slots__属性
一句话说明 __slots__是用来限制实例的属性的,__slots__可以规定实例是否应该有__dict__属性:__slots__不能限制类的属性. 只有__slots__列表内的这些变量名可赋值 ...
- VS Extension: Open Web Address with Visual Studio Browser
使用VS 打开链接 using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; ... public ...
- 【弱省胡策】Round #6 String 解题报告
感觉这个题好神啊. 首先我们只管 $a = b$ 的情况,那么我们自然就可以把这个串对 $a$ 取模,然后用 KMP 求出能弄出几个其他的 B 串. 具体就是把串先倍长,然后倒过来,然后求 $Next ...
- require backbone 移动
http://www.gafish.net/archives/1422 http://www.w3ctech.com/2012/mobile/schedule http://cavenfeng.ite ...