单点时限: 2.0 sec
内存限制: 256 MB
Kid has got the diamond ring after he got the password. After that he tried to escape as quickly as possible. Now give you the map, point 0 stands for Kid, point 1 stands for the exit.
输入格式
There are n (2<=n<=100) points (from 0 to n-1), r roads, given in a line. Different distances between points are given, one line for each road with three integers s, e, l(0<=s, e<=n-1, 0<l<=1000), stands for the road between the point s and e, length l.
输出格式
Please find out the shortest way for Kid to escape. Just output the length of the way you’ve found.
样例
input
5 6
0 2 2
0 3 2
2 3 3
0 4 12
3 1 34
4 1 24
output
36
思路:Dijkstra单元最短路径
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct g {
int x;
int cost;
friend bool operator < (g a,g b) {
return a.cost>b.cost;
}
};
const int inf=100000;
bool visit[100]= {false};//是否访问
bool visit1[100]= {false};//判断是否入队
int d[100];
int n;
vector<g>v[100];
void Dijkstra_1(int s) {
fill(d,d+100,inf);
d[s]=0;
for(int i = 0; i < n; i++) {
int u=-1,Min=inf;
for(int j = 0; j < n; j++) {
if(visit[j]==false&&Min>d[j]) {
u=j;
Min=d[j];
}
}
if(u==-1)
return ;
visit[u]=true;
for(int i = 0; i < v[u].size(); i++) {
int w=v[u][i].x;
int cost=v[u][i].cost;
if(visit[w]==false&&d[u]+cost<d[w]) {
d[w]=d[u]+cost;
}
}
}
}
void Dijkstra_2(int s) {//优先队列实现
fill(d,d+100,inf);
d[s]=0;
priority_queue<g>q;
g G;
G.x=s;
G.cost=0;
q.push(G);
visit1[s]=true;//入队
while(!q.empty()) {
g f=q.top();
q.pop();
visit1[f.x]=false;//不在队列
for(int i = 0; i < v[f.x].size(); i++) {
int u=v[f.x][i].x;
int cost=v[f.x][i].cost;
if(visit[u]==false&&d[f.x]+cost<d[u]) {
d[u]=d[f.x]+cost;
if(visit1[u]==false) {//为入队
g m;
m.x=u;
m.cost=v[f.x][i].cost;
q.push(m);
visit1[u]=true;
}
}
}
}
}
int main() {
int m;
cin>>n>>m;
for(int i = 0; i < m; i++) {
int a,b,cost;
cin>>a>>b>>cost;
g G;
G.x=b;
G.cost=cost;
v[a].push_back(G);
G.x=a;
v[b].push_back(G);
}
Dijkstra_2(0);
cout<<d[1];
// for(int i = 0; i < n; i++)
// cout<<d[i]<<" ";
return 0;
}
题目描述 小 x伤心的走上了 Star way to heaven。 到天堂的道路是一个笛卡尔坐标系上一个 n*m的长方形通道 顶点在0,0 和 。 小 n,m 从最左边任意一点进入,从右边任意一点走到天堂,最左最右的距离n为 ,上下边界距离m为 。 其中长方形有 k个 ,每个k 都有一个整点坐标,star的大小可以忽略不计。 每个star 以及长方形上下两个边缘宇宙的边界都有引力,所以为了成功到...
LeetCode——1769. 移动所有球到每个盒子所需的最小操作数[Minimum Number of Operations to Move All Balls to Each Box][中等]——分析及代码[Java] 一、题目 二、分析及代码 1. 单调递推 (1)思路 (2)代码 (3)结果 三、其他 一、题目 有 n 个盒子。给你一个长度为...
I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na1,a2,⋯,an 和 m1,m2,⋯,mnm_1,m_2,\cdots ,m_nm1,m2,⋯,mn,求一个最小的正整数 xxx,满足 ∀i&isin...
今天又是快快乐乐学习数论的一天~~~ 这次的中国剩余定理是不限定模数互质的。 我们先考虑:只有两个数该怎么处理? 可以得到:x=r1+k1∗m1x = r1 + k1 * m1x=r1+k1∗m1 x=r2+k2∗m2x = r2 +...
LIST TO ARray...
A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she jumped from the po...
The Way to Home A frog lives on the axis Ox and needs to reach home which is in the point n. She starts from the point 1. The frog can jump to the right at a distance not more than d. So, after she ju...
Android 使用Lottie的三个小技巧 Shawn 文章目录 Android 使用Lottie的三个小技巧 I 开启硬件加速 II 通过添加AnimatorListener来控制动画行为 III 通过设置播放速度来实现动画倒放 I 开启硬件加速 开启硬件加速是个提升lottie动画表现效果的一个好办法,在我的老手机上,不开硬件加速就跟幻灯片一样. II 通过添加AnimatorListene...
Java8中Comparator的接口源码如下,这其中包括了@FunctionalInterface注解、defult关键字和(para1, para2) -> {statement}语法。 在Map中有一个Entry的接口,其中有一个方法是: 基于上面两段代码对Lambda进行一个简单的介绍: 第二段代码等同于: @FunctionalInterface注解 新注解,用来标注这是一个&ld...
LINK1 LINK2 题目大意 给你一个无向连通图,让你给一些点染上黑色,需要满足染色之后,断开任意一个节点,要满足任意一个联通块中剩下的节点中至少有一个黑点 思路 一开始想的是把每一个点双联通分量都把除了割点的size乘上 然后发现随手卡掉 然后发现一下性质 首先所有相邻点双联通分量一定有公共的割点 如果一个双联通分量里面只包含了一个割点,那么如果断掉这个割点那么这个双联通分量就被孤立了 所以...