题目链接: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. 【转】TCP/IP协议——ARP详解

    本文主要讲述了ARP的作用.ARP分组格式.ARP高速缓存.免费ARP和代理ARP. 1.学习ARP前要了解的内容 建立TCP连接与ARP的关系 应用接受用户提交的数据,触发TCP建立连接,TCP的第 ...

  2. ASP.NET Core Windows 环境配置

    ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我们将介绍 ASP.NET Core 中的一些新的概念和它们是如何帮助我们开发现代化的 Web 应用程序 尽管 ASP. ...

  3. printkd

    #include <linux/fs.h> #include <asm/uaccess.h> #include <linux/namei.h> #include & ...

  4. 微信小程序之页面打开数量限制

    无论是在小程序还是APP中,打开一个页面其实就是创建了一个新的View对象,一层层叠加的.当点击页面的回退按钮就是把当前页面关闭. 这个过程中会涉及到一个问题,就是打开页面的数量.在某些设计下,比如一 ...

  5. Linux系统运维笔记,CentOS 7.4防火墙配置

    1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态 firewall-cmd --state 3.开启.重启.关闭.firewall ...

  6. win10 连接samba 账号密码不正确,win7可以访问

    1.本地安全策略,本地策略-安全选项,需要修改成默认的值的修改方式: 查找注册表浏览到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\LSA ...

  7. sqlite删除数据

    sqlite删除数据 某日, 在使用sqlite的时候发现查询速度极慢, 查看了一下文件大小, sqlite数据库文件已经达到了 22G! 对于一个文件数据库来说, 已经相当致命了,先不说后面如果解决 ...

  8. 【leetcode】字母异位词分组

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...

  9. 集合类源码(五)Collection之BlockingQueue(LinkedTransferQueue, PriorityBlockingQueue, SynchronousQueue)

    LinkedTransferQueue 功能 全名 public class LinkedTransferQueue<E> extends AbstractQueue<E> i ...

  10. THUPC2019/CTS2019/APIO2019自闭记

    自闭了,自闭选手不配拥有游记.