题意:给出一堆双向路,求从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. 雷林鹏分享:Ruby 语法

    Ruby 语法 让我们编写一个简单的 Ruby 程序.所有的 Ruby 文件扩展名都是 .rb.所以,把下面的源代码放在 test.rb 文件中. #!/usr/bin/ruby -w puts &q ...

  2. JDBC 与 Bean Shell的使用(一)获取值,并且传递

    1.在使用Jmeter进行接口测试的时候,会使用到JDBC,连接数据库,操作数据库其得到的数据后续操作需要使用,这里我们使用了BeanShell的概念来获取JDBC的返回值 如下说明了联合使用的2种方 ...

  3. English trip WeekEnd-Lesson 2018.11.10

    本周末上了三节课,做个小结吧\(^o^)/~: [102] 新概念一早读 - 27 - 28        Teacher: March Mrs. Smith's living room is lar ...

  4. 第7章使用请求测试-测试API . Rspec: everyday-rspec实操。

    测试应用与非人类用户的交互,涵盖外部 API 7.1request test  vs feature test 对 RSpec 来说,这种专门针 对 API 的测试最好放在 spec/requests ...

  5. android--------Dagger2介绍与简单使用(一)

    1:Dagger2是啥 Dagger是为Android和Java平台提供的一个完全静态的,在编译时进行依赖注入的框架,原来是由Square公司维护的然后现在把这堆东西扔给Google维护了. 一般的I ...

  6. Confluence 6 从外部目录中同步数据手动同步缓存

    你可以通过单击用户目录(User Directories)界面中的同步(Synchronize)按钮,手动进行同步.如果一个同步进程已经正在同步的过程中的话,你就不能在上一个同步进程完成之前重新进行同 ...

  7. UVA-12186 Another Crisis (树状DP)

    题目大意:一家工厂,一个老板(编号为0),n个工人(编号1~n),其中,有的工人是中层领导,管辖一部分其他工人.现在大家要签署一份加薪申请书,但是按照规定不能越级上访,所以只能通过一层层的中间领导传到 ...

  8. Vue--关于点击当前路由,视图无法更新的解决方案

    转自:https://juejin.im/post/593121aa0ce4630057f70d35 问题的根源: 用户点击当前高亮的路由并不会刷新view,因为vue-router会拦截你的路由,它 ...

  9. 根据id来实现小程序tab切换,

    本例根据绑定id来实现tab切换,但本例仍有缺陷,用for循环数据,无法实现切换.如有大神能够有更好方法,欢迎留言更正 WXML: <view class="tab"> ...

  10. 纯js倒计时效果(交流加群:452892873)(本群每天都更新学习资料)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...