题目连接:http://poj.org/problem?id=2387

题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离。

分析:最短路裸题。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-9
#define N 1010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct node
{
int v,w;
node(){}
node(int v,int w):v(v),w(w){}
bool operator<(const node &a)const
{
return w>a.w;
}
};
int dp[N],vis[N],n,m;
vector<node>g[N];
int dij()
{
priority_queue<node>que;
while(!que.empty())que.pop();
FILL(dp,0x3f);FILL(vis,);
node cur,nxt;
cur.v=n;cur.w=;
dp[n]=;
que.push(cur);
while(!que.empty())
{
cur=que.top();que.pop();
int x=cur.v;
if(vis[x])continue;
vis[x]=;
for(int i=,sz=g[x].size();i<sz;i++)
{
nxt=g[x][i];
int v=nxt.v,w=nxt.w;
if(dp[x]+w<dp[v])
{
dp[v]=dp[x]+w;
que.push(node(v,dp[v]));
}
}
}
return dp[];
}
int main()
{
int u,v,w;
while(scanf("%d%d",&m,&n)>)
{
for(int i=;i<=n;i++)g[i].clear();
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
g[u].push_back(node(v,w));
g[v].push_back(node(u,w));
}
printf("%d\n",dij());
}
}

poj2387(最短路)的更多相关文章

  1. poj2387 最短路

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

  2. POJ2387(最短路入门)

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

  3. 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 ...

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

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

  5. poj2387 初涉最短路

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

  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. kali之ARP欺骗获取图片流

    其实很简单,就两步: 1. 后接三个参数: 网卡:eth0    网关:10.0.0.1    攻击目标:10.0.0.128 2.启动监听 会弹出一个框 里面就会显示攻击目标通过浏览器访问的页面上的 ...

  2. 11586 - Train Tracks

    Problem J: Train Tracks Andy loves his set of wooden trains and railroad tracks. Each day, Daddy has ...

  3. windows lwp 乱码问题

    use LWP::UserAgent; use Encode; my $ua = LWP::UserAgent->new; $phone=$ARGV[0]; $cc=$ARGV[1]; $ua- ...

  4. perl 继承概述

    <pre name="code" class="html">[root@wx03 test]# cat Horse.pm package Horse ...

  5. 单选按钮易忽略的Group属性

    Group就其意思就是一组的意思.就是说用于选择多个控件组合,选了TRUE后,你就可以为这组新建一个变量.把一组控件当一个控件来使用.例如多个单选按钮用group属性,这样你就可以用一个变量来管理这些 ...

  6. 重载new delete操作符是怎么调用的

    自定义的new操作符是怎么对英语new 一个对象的?自定义的delete操作符什么情况下得到调用?new一个对象时出现异常需要我操心内存泄露吗?下面的一个例子帮我们解开所有的疑惑. 1. 调用规则   ...

  7. ASP.NET - 记录错误日志

    不需要像log4net/Nlog/Common Logging配置,简单好用. 不用增加声明logger对象,可记录当前执行状况. 可以定义 维护功能模板的开发人员,以便用功能模块对于开发人员. 出处 ...

  8. ajax和json详解

    responseText  属性以字符串形式返回HTTP响应. responseXML  属性以XML形式返回HTTP响应. JSON.stringify 函数 (JavaScript)  将 Jav ...

  9. mysql 父子结构排序

    项目中常常会遇到父子结构显示的问题,不同的数据库有不同的写的方式,比方SqlServer中用with union 实现.而Mysql则没有这么方便的语句. 例如以下category表.食品有pizaa ...

  10. SDL Guide 中文译版

    SDL即Simple DirectMedia Layer,类似DirectX,是完整的游戏.多媒体开发包,但不同的是它跨越几乎所有的平台,有各种语言的接口,多种语言的文档,而这一切都是广大志愿者完成的 ...