题意:给出一堆双向路,求从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. ZendFramework中实现自动加载models

    最近自学Zendframework中,写Controller的时候总要require model下的类文件,然后才能实例化,感觉非常不爽 Google了许久,找到个明白人写的方法不错,主要就是修改ap ...

  2. AsyncTask用法和异步加载图片

    AsyncTask:是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI ...

  3. PHP函数总结 (三)

    <?php/** * PHP变量的范围 * 1.局部变量(内部变量) * 在函数内部声明的变量,作用域仅限于函数内部,参数也是局部变量:执行完毕后函数内部的变量都被释放 * 若需要使用函数内的变 ...

  4. Codeforces Round #364 (Div. 1) (差一个后缀自动机)

    B. Connecting Universities 大意: 给定树, 给定2*k个点, 求将2*k个点两两匹配, 每个匹配的贡献为两点的距离, 求贡献最大值 单独考虑每条边$(u,v)$的贡献即可, ...

  5. Python多线程多进程

    一.线程&进程 对于操作系统来说,一个任务就是一个进程(Process),比如打开一个浏览器就是启动一个浏览器进程,打开一个记事本就启动了一个记事本进程,打开两个记事本就启动了两个记事本进程, ...

  6. [LeetCode] 41. First Missing Positive ☆☆☆☆☆(第一个丢失的正数)

    Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...

  7. MVC,重写AuthorizeAttribute实现自己的权限验证(一)

    我们要实现下面的效果,某个controller,只允许某几个角色访问(admin,user,document controller) [MyAuthorize(Roles = "Admin, ...

  8. PHP:第二章——PHP中的break一continue一return语句

    知识点一:break语句     break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行.     break 可以接受一个可选的数字参数来决定跳出 ...

  9. POJ 2896 AC自动机 or 暴力

    DESCRIPTION :大意是说.给你n个代表病毒的字符串.m个表示网站的字符串.让你计算有多少个网站被病毒感染了.被那些病毒感染了. 刚开始就想暴力.然而,忽略了条件:每个网站最多有三个病毒.于是 ...

  10. Awk 从入门到放弃(2)– 分隔符 学习笔记

    转:http://www.zsythink.net/archives/1336 学习输入分隔符FS及输出分隔符OFS 通过-v 修改内置的变量,在$1 $2 之间不指定 ‘,’, 会做合并输出.