#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)的更多相关文章

  1. POJ 2449Remmarguts' Date K短路模板 SPFA+A*

    K短路模板,A*+SPFA求K短路.A*中h的求法为在反图中做SPFA,求出到T点的最短路,极为估价函数h(这里不再是估价,而是准确值),然后跑A*,从S点开始(此时为最短路),然后把与S点能达到的点 ...

  2. 最短路模板[spfa][dijkstra+堆优化][floyd]

    借bzoj1624练了一下模板(虽然正解只是floyd) spfa: #include <cstdio> #include <cstring> #include <alg ...

  3. 最短路模板|堆优化Dijkstra,SPFA,floyd

    Ⅰ:Dijkstra单源点最短路 1.1Dijkstra const int MAX_N = 10000; const int MAX_M = 100000; const int inf = 0x3f ...

  4. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  5. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  6. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  7. poj 2499第K短路模板

    第k*短路模板(单项边) #include <iostream> #include <cstdio> #include <algorithm> #include & ...

  8. K短路模板POJ 2449 Remmarguts' Date

      Time Limit: 4000MS   Memory Limit: 65536K Total Submissions:32863   Accepted: 8953 Description &qu ...

  9. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

随机推荐

  1. 前端JS Excel解析导入

    本文转载自:https://www.cnblogs.com/yinqingvip/p/6743213.html 需要用到js-xlsx:下载地址:js-xlsx <!DOCTYPE html&g ...

  2. 【POJ2230】Watchcow

    题目大意:给定一个 N 个点,M 条边的无向图,要求不重复地经过每条边两次,并且从 1 号节点出发最后回到 1 号节点,求一条路径. 题解:不重复地经过两次这个操作很容易地通过无向图的建边方式来实现, ...

  3. bzoj3900 交换茸角

    题目链接 思路 看到n比较小,可以状压. 可以先考虑什么情况下会无法平衡.显然就是排完序之后两两相邻的不能满足小于等于c的限制. 状态.用f[i]来表示i集合中的鹿完成交换所需要的次数. 预处理.无法 ...

  4. [agc016B][Colorful Hats]

    题目链接 思路 首先,如果没人说谎那么序列中肯定只有一大一小两种数,假设大的数为x,小的数为y.因为对于每个人只有两种情况,要么自己与除自己外的某个人拥有相同的颜色,此时总颜色数就是这个人所能看到的颜 ...

  5. JavaScript(JS)之Javascript对象BOM,History,Location,Function...(二)

    https://www.cnblogs.com/haiyan123/p/7594046.html 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创 ...

  6. Windows android SDK环境配置及判断安装成功

  7. Gym 101915

    Gym - 101915A  Printing Books 题意:有一本书,从第X页开始,一共用了n位数字,求此书一共多少页.99就是两位数字,100就是三位数字. 思路:直接模拟即可,我用了一个hi ...

  8. codesmith生成的结果页不显示,问题在于第一行的文件头

    在于这里: TargetLanguage="C#",这个能增加cs的格式

  9. E212: Can't open file for writing

    意思是不能保存. 原因是权限不够,普通用户用vi 进行不了保存,需要使用超级用户才可以 命令:sudo su     转换成超级用户 vi hello       打开文件 :wq 即可保存退出

  10. ubuntu下cmake编译opencv 3.4.3源码;

    在进行编译前,准备工作: 1)  到opencv官网下载源码(https://opencv.org/releases.html): 2)  安装qt(http://download.qt.io/); ...