题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行

dij:

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
const int INF=0x3f3f3f3f; int head[],dist[],point[],val[],next[],size; void add(int a,int b,int v){
int i;
for(i=head[a];~i;i=next[i]){
if(point[i]==b){
if(val[i]>v)val[i]=v;
return;
}
}
point[size]=b;
val[size]=v;
next[size]=head[a];
head[a]=size++;
} struct cmp{
bool operator()(pii a,pii b){
return a.first>b.first;
}
}; void dij(int s){
int i;
priority_queue<pii,vector<pii>,cmp>q;
q.push(make_pair(,s));
memset(dist,-,sizeof(dist));
dist[s]=;
while(!q.empty()){
pii p=q.top();
q.pop();
if(p.first>dist[p.second])continue;
for(i=head[p.second];~i;i=next[i]){
int j=point[i];
if(dist[j]==-||dist[j]>p.first+val[i]){
dist[j]=p.first+val[i];
q.push(make_pair(dist[j],j));
}
}
}
printf("%d\n",dist[]);
} int main(){
int t,n;
while(scanf("%d%d",&t,&n)!=EOF){
int i,j;
size=;
memset(head,-,sizeof(head));
for(i=;i<=t;i++){
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
add(a,b,v);
add(b,a,v);
}
dij(n);
}
return ;
}

dij

spfa:

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; int head[],dist[],next[],point[],val[],size;
bool vis[]; void add(int a,int b,int v){
int i;
for(i=head[a];~i;i=next[i]){
if(point[i]==b){
if(val[i]>v)val[i]=v;
return;
}
}
point[size]=b;
val[size]=v;
next[size]=head[a];
head[a]=size++;
} void spfa(int s,int p){
memset(vis,,sizeof(vis));
memset(dist,-,sizeof(dist));
queue<int>q;
vis[s]=;
dist[s]=;
q.push(s);
while(!q.empty()){
int i,t=q.front();
vis[t]=;
q.pop();
for(i=head[t];~i;i=next[i]){
int j=point[i];
if(dist[j]==-||dist[j]>dist[t]+val[i]){
dist[j]=dist[t]+val[i];
if(!vis[j]){
q.push(j);
vis[j]=;
}
}
}
}
printf("%d\n",dist[p]);
} int main(){
int t,n;
while(scanf("%d%d",&t,&n)!=EOF){
int i,j;
memset(head,-,sizeof(head));
size=;
for(i=;i<=t;i++){
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
add(a,b,v);
add(b,a,v);
}
spfa(n,);
}
return ;
}

spfa

poj2387 最短路的更多相关文章

  1. POJ2387(最短路入门)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38556   Accepted ...

  2. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  3. POJ-2387(原始dijkstra求最短路)

    Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...

  4. poj2387 初涉最短路

    前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...

  5. poj2387(最短路)

    题目连接:http://poj.org/problem?id=2387 题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离. 分析:最短路裸题. #include ...

  6. 【POJ2387】Til the Cows Come Home (最短路)

    题面 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible bef ...

  7. POj2387——Til the Cows Come Home——————【最短路】

    A - Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & ...

  8. POJ-2387 Til the Cows Come Home ( 最短路 )

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  9. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

随机推荐

  1. 15分钟入门lua

    目录:[ - ] -- 1. Variables and flow control. -- 2. Functions. -- 3. Tables. -- 3.1 Metatables and meta ...

  2. android--------ExpandableListView的使用多级列表

    多级列表ExpandableListView 扩展列表能够显示一个指示在每项显示项的当前状态(状态通常是一个扩展的组,组的孩子,或倒塌,最后一个孩子).使用setchildindicator(draw ...

  3. splay训练

    1, CF 455D 2, CF 420D 3, CF 414E

  4. Imbalance Value of a Tree CodeForces - 915F

    链接 大意: 给定树, 求树上所有链上最大值最小值之差 817D的树上版本, 用并查集维护即可. 817D由于是链的情况并查集不必压缩路径即可达到均摊$O(n)$, 该题必须压缩, 复杂度$O(nlo ...

  5. 弗洛伊德算法(Floyd算法)

    原博来自http://www.cnblogs.com/skywang12345/ 弗洛伊德算法介绍 和Dijkstra算法一样,弗洛伊德(Floyd)算法也是一种用于寻找给定的加权图中顶点间最短路径的 ...

  6. ZOJ 2770 差分约束+SPFA

    Burn the Linked Camp Time Limit: 2 Seconds      Memory Limit: 65536 KB It is well known that, in the ...

  7. linux下常用的截图、录屏工具

    录屏: 在linux下常用的录屏工具有5种,可以baidu或者google下喔,我选用的是recordMydesktop,使用非常方便,用时注意先把每秒桢数调高,否则效果必然很差. 在ubuntu下可 ...

  8. Andriod给textview文本关键字循环标亮加粗

    在开发中,搜索到得关键字信息在展示时,通常需要标亮加粗,如下图(截取自蓝鲸医生助手搜索后的结果) 在文本中,关键字是“嘎”,所有“嘎”字都标亮加粗,标亮就是换种颜色.这里就要用到SpannableSt ...

  9. POJ 3481 SBT做法

    第三次做此题.. 不解释啦. 不过变成用SBT来做啦! SBT好处在于能够保证树的高度为lgn,真真正正的平衡二叉树. 因此删除,插入操作与普通二叉树几乎相同. #include <cstdio ...

  10. 多态性&& 虚函数 && 抽象类

    http://www.cnblogs.com/CaiNiaoZJ/archive/2011/08/11/2134673.html 多态性 指相同对象收到不同消息或不同对象收到相同消息时产生不同的实现动 ...