题目连接: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. 讲解下for循环的用法,加深记忆

    引子 这是一段很简单的代码,但是即便是这么简单的东西,这里我们还是需要说一下. 关于for循环整个执行流程就是,先执行var i=10,然后到了第二个语句,判断10是否大于0,很明显为true,所以此 ...

  2. ARC内存使用注意事项

    官方介绍: https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/M ...

  3. boost::asio设置同步连接超时

    boost::asio设置同步连接超时   CSDN上求助无果,只好用自创的非主流方法了.asio自带的例子里是用deadline_timer的async_wait方法来实现超时的,这种方法需要单独写 ...

  4. boost锁的概述

    ●     boost锁的概述 boost库中提供了mutex类与lock类,通过组合可以轻易的构建读写锁与互斥锁. ▲     mutex对象类 mutex类主要有两种:boost::mutex,b ...

  5. Computational Geometry Template

    顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...

  6. C语言深度解剖读书笔记(6.函数的核心)

    对于本节的函数内容其实就没什么难点了,但是对于函数这节又涉及到了顺序点的问题,我觉得可以还是忽略吧. 本节知识点: 1.函数中的顺序点:f(k,k++);  这样的问题大多跟编译器有关,不要去刻意追求 ...

  7. Mahout-Pearson correlation的实现

    计算公式: 并通过以下代码对Mahout in Action的结果进行了验证: 代码例如以下: ` package com.example.mahout; public class TestColl ...

  8. 与众不同 windows phone (2) - Control(控件)

    原文:与众不同 windows phone (2) - Control(控件) [索引页][源码下载] 与众不同 windows phone (2) - Control(控件) 作者:webabcd介 ...

  9. 打开excel2007文档时显示“excel无法打开文件~$book.xltm”

    此问题的出现是因为意外情况导致Excel临时文件没有删掉,把C:\Program Files\Microsoft Office\Office12\XLSTART里面的临时文件“~$book”给删除就好 ...

  10. 移动App-UI配制篇

    杂志app-UI配制篇 背景 现在公司有需求是关于杂志app,里面每个页面可能有不同的展现方式,不同的内容,不同的操作方式.那么这里就有一个需求就是根据用户定制这些不同的展现方式,不同的内容,不同的操 ...