最短路模板(SPFA POJ2387)
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <bitset>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define MAXN 1010100
#define LL long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll __int64
#define INF 0x7fffffff
#define cs(s) freopen(s,"r",stdin)
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-10
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
//head
const int maxn=2021;
int n,m,inq[maxn],dis[maxn];
vector<pair<int,int> >v[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;i++){
int a,b,w;
cin>>a>>b>>w;
v[a].pb(mp(b,w));
v[b].pb(mp(a,w));
}
queue<int>p;
p.push(1);
inq[1]=1;
for(int i=1;i<=max(n,m);i++)dis[i]=INF;
dis[1]=0;
while(!p.empty()){
int now=p.front();
p.pop();
inq[now]=0;
for(int i=0;i<v[now].size();i++){
int ne=v[now][i].fi;
if(dis[now]+v[now][i].se<dis[ne]){
dis[ne]=dis[now]+v[now][i].se;
if(!inq[ne]){
inq[ne]=1;
p.push(ne);
}
}
}
}
cout<<dis[m];
return 0;
}
最短路模板(SPFA POJ2387)的更多相关文章
- POJ 2449Remmarguts' Date K短路模板 SPFA+A*
K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...
- 最短路模板[spfa][dijkstra+堆优化][floyd]
借bzoj1624练了一下模板(虽然正解只是floyd) spfa: #include <cstdio> #include <cstring> #include <alg ...
- 最短路模板|堆优化Dijkstra,SPFA,floyd
Ⅰ:Dijkstra单源点最短路 1.1Dijkstra const int MAX_N = 10000; const int MAX_M = 100000; const int inf = 0x3f ...
- 模板C++ 03图论算法 1最短路之单源最短路(SPFA)
3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- poj 2499第K短路模板
第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...
- K短路模板POJ 2449 Remmarguts' Date
Time Limit: 4000MS Memory Limit: 65536K Total Submissions:32863 Accepted: 8953 Description &qu ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
随机推荐
- Vim保存时权限不足
保存时权限不足,由于打开时忘记在命令前添加sudo.我们并不需要放弃修改,从新以root权限打开 解决方案 命令模式使用:w !sudo tee %提权,保存
- 浏览器直接显示html代码,不解析代码
在某些时候,我们可能因为某些特殊的原因,不想让浏览器解析html代码. 1,把代码放到js中,如下 <script type='text/html' style='display:block'& ...
- ERRORS: ?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
Ubuntu下,运行django项目的时候失败,报错: (env36) root@JD:~/xueyiwang# python manage.py runserver 0.0.0.0:8000 Per ...
- Web 服务器被配置为不列出此目录的内容。
vs2015运行类库程序,遇到问题如下图, 最可能的原因: 没有为请求的 URL 配置默认文档,并且没有在服务器上启用目录浏览. 解决方法: 确认网站或应用程序配置文件中的 configuration ...
- app软件遵循的规范
http://www.jianshu.com/p/a2a4c18c1900 https://wenku.baidu.com/view/ecb09b07a4e9856a561252d380eb6294d ...
- Vuex速学篇:(2)利用state保存新闻数据
回顾 以前我们在做这个新闻列表的时候,是一个写死的数据 export default{ data(){ return{ newslist:[ {newsid:"101",pubti ...
- Luogu P2468 [SDOI2010]粟粟的书架
一道二合一的题目.两部分思维难度都不太高,但是也都很巧妙.尤其是主席树的\(50\)分,由于本人初学主席树,所以没有见过主席树上二分的套路,就被小小的卡了一下.. \(n <= 200\) \( ...
- 【3D动画建模设计工具】Maxon Cinema 4D Studio for Mac 20.0
图标 Icon 软件介绍 Description Maxon Cinema 4D Studio R20 ,是由德国公司Maxon Computer一款适用于macOS系统的3D动画建模设计工具,是 ...
- 信用评分卡 (part 3of 7)
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- jvm垃圾收集器与内存分配策略
一.垃圾回收 1.对象是否已经变为垃圾 1.1.引用计数法:给对象添加一个引用计数器,每当有地方引用它时,计数器就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 这 ...