题意:给出一堆双向路,求从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. Python 爬虫-BeautifulSoup

    2017-07-26 10:10:11 Beautiful Soup可以解析html 和 xml 格式的文件. Beautiful Soup库是解析.遍历.维护“标签树”的功能库.使用Beautifu ...

  2. MyBatis3-基于注解的示例

    在基于注解的示例中,可以简化编写XML的过程,全部采用注解方式进行编写,并在注解上写SQL语句,语句和XML的语句保持一致,并且可以省略掉XML文件不用引入的好处.但还有一点,基于注解的方式还没有百分 ...

  3. Eclipse 中 SDK无法更新---解决方法

    在SDK Manager -> tools -> options中: HTTP Proxy Server: mirrors.neusoft.edu.cn HTTP Proxy Port: ...

  4. [Java学习] Java虚拟机(JVM)以及跨平台原理

    相信大家已经了解到Java具有跨平台的特性,可以“一次编译,到处运行”,在Windows下编写的程序,无需任何修改就可以在Linux下运行,这是C和C++很难做到的. 那么,跨平台是怎样实现的呢?这就 ...

  5. PyQt5-GUI生成随机生成小工具

    自己修改了代码:实现了自动生成SSN,手机号和姓名的功能 import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from ...

  6. Confluence 6 LDAP 高级设置

    启用嵌套组(Enable Nested Groups) 为嵌套组启用或禁用支持. 一些目录服务器能够允许你在一个组中定义另外一个组.在这种结构下的用户组称为用户组嵌套.嵌套组的配置能够让子用户组继承上 ...

  7. HDOJ1008

    #include "iostream" using namespace std; int main() { ) { int n; cin >> n; ) break; ...

  8. 第5章——使用 Razor(MVC框架视图引擎)

    Razor 是MVC框架视图引擎的名称. 本章提供 Razor 语法的快速教程,以使你能够识别 Razor 表达式. 本章不打算提供 Razor 的完整参考,而将其视为一个语法速成教程.在本书的后续内 ...

  9. 原生JS和jQuery版实现文件上传功能

    <!doctype html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  10. bind出现Address already in use解决方法

    在socket函数和bind函数之间加入一段代码: // 建立服务器端socket if((server_sockfd = socket(AF_INET, SOCK_STREAM, 0))<0) ...