poj2387(最短路)
题目连接: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(最短路)的更多相关文章
- poj2387 最短路
题意:给出一堆双向路,求从N点到1点的最短路径,最裸的最短路径,建完边之后直接跑dij或者spfa就行 dij: #include<stdio.h> #include<string. ...
- POJ2387(最短路入门)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38556 Accepted ...
- 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 ...
- POJ-2387(原始dijkstra求最短路)
Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...
- poj2387 初涉最短路
前两天自学了一点点最短路..看起来很简单的样子... 就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~ 松弛虽然会但是用的十分之不熟练... 代码 ...
- 【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 ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- 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 ...
- poj2387 spfa求最短路
//Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...
随机推荐
- linux脚本: makefile以及链接库
Linux makefile 教程 非常详细,且易懂 http://blog.csdn.net/liang13664759/article/details/1771246 //sort.c #incl ...
- MySQL 暂时文件夹
MySQL数据文件夹/data/mysql所在的上层文件夹/data磁盘空间不足导致MySQL启动失败,所以清理了/data文件夹下除了mysql子文件夹外的其它无用文件夹.重新启动发现还是失败.检查 ...
- Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等)
当前位置: 首页 > 编程社区 > Swift > Swift - 常用文件目录路径获取(Home目录,文档目录,缓存目录等) Swift - 常用文件目录路径获取(Home目录,文 ...
- SVM(三)—Kernels(核函数)
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 内容整理中...
- webdynpro的select_option示例
需求,输入航线代码和航线编号区间,查询记录存在表中显示,并不是alv显示 1.使用组件WDR_SELECT_OPTIONS. 2.在组件控制器中加入以下组件 3.在视图属性中也添加该组件 4.创建节点 ...
- Servlet的学习之Session(5)
在上一篇中我们介绍了如果使用Session来做一个简单的用户登录案例,在本篇中我们继续使用Session技术来做一个防止表单重复提交的案例. 这是一个很重要的知识点,在很多框架中都有防止表单重复提交的 ...
- c#1所搭建的核心基础之委托
本文将对c#1的委托进行详细探索 委托(delegate) 注 delegate:vt.委派代表; 授权给; [法律]债务转移; 委托作用:在恰当的时间执行一系列操作 1.简单委托的构成 声明委 ...
- 14.6.7?Limits on InnoDB Tables InnoDB 表的限制
14.6.7?Limits on InnoDB Tables InnoDB 表的限制 警告: 不要把MySQL system tables 从MyISAM 到InnoDB 表. 这是不支持的操作,如果 ...
- JavaScript快速入门(五)——表达式运算
赋值运算 赋值运算的形式为左值 = 右值.如果同个表达式中有多个赋值运算,则从右到左运算.例如: a = b = c; // 和下面两行等价 b = c; a = b; 另外一种赋值运算的形式叫做复合 ...
- 【转】有效修改max open files/ulimit -n
[转]有效修改max open files/ulimit -n_追梦20121222_新浪博客 [转]有效修改max open files/ulimit -n (2011-11-18 0 ...