题目链接: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. ASP.NET Core Windows 环境配置

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

  2. 【转】web.xml详解

    转载:https://www.cnblogs.com/vanl/p/5737656.html. 一:web.xml加载过程 简单说一下,web.xml的加载过程.当我们启动一个WEB项目容器时,容器包 ...

  3. Vue实际中的应用开发【分页效果与购物车】

    作者 | Jeskson 来源 | 达达前端小酒馆 分页组件 首先来创建项目: 分页组件,做项目不要写动手写代码,要想想业务逻辑,怎么写,如何写才是最好的呈现方式,做项目不急,要先想好整体的框架,从底 ...

  4. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. [LeetCode] 161. One Edit Distance 一个编辑距离

    Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 pos ...

  6. [LeetCode] 25. Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k  ...

  7. IDEA中阿里P3C插件的安装与使用

    在线安装: 离线安装: https://plugins.jetbrains.com/plugin/10046-alibaba-java-coding-guidelines/versions

  8. 关于一些规范:main()函数的返回值 mingw和mingw-w64编译器的区别

    深度剖析c语言main函数---main函数的返回值 - 编程随笔与杂谈 - CSDN博客 https://blog.csdn.net/z_ryan/article/details/80979008 ...

  9. SPA框架 Angular、React、Vue

    指尖前端重构(React)技术调研分析   摘要:重构前的技术文档调研与分析,包括技术选型为什么选择react,应用过程中的注意事项等. 一.为什么选择React React是当前前端应用最广泛的框架 ...

  10. sort和sorted

    sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,而内建函数 ...