题目链接: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的更多相关文章

  1. Day4 - L - Tram POJ - 1847

    Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In e ...

  2. Tram POJ - 1847 spfa

    #include<iostream> #include<algorithm> #include<queue> #include<cstdio> #inc ...

  3. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  4. 最短路 || POJ 1847 Tram

    POJ 1847 最短路 每个点都有初始指向,问从起点到终点最少要改变多少次点的指向 *初始指向的那条边长度为0,其他的长度为1,表示要改变一次指向,然后最短路 =========高亮!!!===== ...

  5. poj 1847 最短路简单题,dijkstra

    1.poj  1847  Tram   最短路 2.总结:用dijkstra做的,算出a到其它各个点要改向的次数.其它应该也可以. 题意: 有点难懂.n个结点,每个点可通向ki个相邻点,默认指向第一个 ...

  6. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  7. [最短路径SPFA] POJ 1847 Tram

    Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14630 Accepted: 5397 Description Tra ...

  8. POJ 1847 Tram (最短路)

    Tram 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/N Description Tram network in Zagreb ...

  9. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

随机推荐

  1. nowcoder911L 最优子区间

    题目链接 思路 用\(f(i,j)\)表示前i个元素,以i为右端点,j为左端点时的答案. 用个"区间修改,单点查询"的线段树维护出第二维.在从左往右枚举i的过程中.将\([lst_ ...

  2. Linux性能优化实战学习笔记:第四十七讲

    一.上节回顾 上一节,我们梳理了,应用程序容器化后性能下降的分析方法.一起先简单回顾下.容器利用 Linux 内核提供的命名空间技术,将不同应用程序的运行隔离起来,并用统一的镜像,来管理应用程序的依赖 ...

  3. vue_02day练习

    目录 vue_02day 作业 vue 框架 :成绩单的实现 v-for 与 v-if 联用: 3.添加筛选规则(深入) : 留言功能的页面实现: vue_02day 作业 1. 先有一下成绩单数据 ...

  4. KMP 串的模式匹配 (25分)

    给定两个由英文字母组成的字符串 String 和 Pattern,要求找到 Pattern 在 String 中第一次出现的位置,并将此位置后的 String 的子串输出.如果找不到,则输出“Not ...

  5. Apollo:微服务架构下的配置管理

    问题背景 在实际工作中,我们的开发环境,测试环境,生产环境对应的 Mysql 数据库,Redis 这些信息都不一样,每个环境都有对应的一套配置,在 Spring Boot 中我们通常会编写多个配置文件 ...

  6. 使用IDEA+vue.js+easyUI的demo

    最近,随便写了几个开发的小例子,自己总结下,留个纪念. 例子1:使用EasyUI做了一个简单界面,服务器和客户端在一起. @Controller @RequestMapping("/demo ...

  7. 工作中常用的Linux命令介绍与实践

    前言 做后端开发的同学,一般都会接触到服务器,而我们现在的系统用的比较多的服务器系统就是linux了,平时多多少少也会接触到一些linux下的shell命令.我们来介绍下linux一些常用的命令和使用 ...

  8. python接口自动化1-http协议简介

    前言 知道接口测试的小伙伴都应清楚http协议了,如果你还不清楚,那么接口测试做不好,自动化接口就更做不好了.那么做接口测试前先了解下http: HTTP协议(HyperText Transfer P ...

  9. Prometheus 与 Alertmanager 通信

    Prometheus 与 Alertmanager 通信 1.编辑Prometheus配置文件配置连接地址:vim prometheus.yml # Alertmanager configuratio ...

  10. jQuery浮窗图片到页面中间的代码兼容移动端

    jQuery浮窗图片到页面中间的代码兼容移动端 <!doctype html> <html> <head> <meta charset="utf-8 ...