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 ...
随机推荐
- linux翻页及字符串搜索操作
向下翻动一屏幕: space, ctrl + f, ctrl + v, ctrl + F 向下翻动半屏: d, ctrl + D 向下翻动一行: 回车, e, j 向上翻动一屏幕: b, ctrl + ...
- python3中pymysql模块的事务操作
try: cursor.execute(sql_1) cursor.execute(sql_2) cursor.execute(sql_3) except Exception a ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- [LeetCode] 378. Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 关于被malloc分配内存的指针
例如创建了一个链表指针p并为其malloc()分配了内存,那么这个指针指向的地方其实是有数据的. 你可以把p->data打印出来,会发现是一个随机值 因为只是分配内存而没有指定data的值,所以 ...
- withDefaultPasswordEncoder() 过时弃用问题
在学springsecurity5.X时,在demo里,内存配置用户的时候,提示withDefaultPasswordEncoder过时,特查看了源码,官方给出的理由是: /** @deprecate ...
- 慕课网springboot博客系统开发(一)----spring initializr的使用 gradle构建项目
spring initializr工具的地址:https://start.spring.io/:通过它可以很方便的创建springboot项目 这里我们选择使用gradle作为项目的构建工具,此spr ...
- CSS3幸运大转盘最简单的写法
点击开始 直接css动画 如果你要自己控制转到哪里 那就多写几个class 根据不同角度 运行不同的class..<pre>.zhuandong{ animation: zhuandong ...
- PHP 函数禁用设置方法
PHP 函数禁用设置方法先找到php.ini 然后搜索disable_function 直接在上面添加你要禁用的函数就可以了 然后记得重启php-fpm(如果装了php-fpm)
- HTTP之URL的快捷方式
URL快捷方式 ==================摘自<HTTP权威指南>======================= WEB客户端可以理解并使用几种URL快捷方式.相对URL是在某职 ...