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> ...
随机推荐
- 浅谈BFC与应用
什么是BFC BFC(Block formatting context)的中文翻译我们一般叫做块级格式化上下文.它是一个独立渲染的区域,规定了内部如何布局,同时不受外界的影响.我们的根元素本身就是一个 ...
- Qt QDebug等重定向到文件中
源地址:http://blog.163.com/soyo_gogogo/blog/static/171414077201392705639321/ Qt的qDebug() 等函数,可以打印出十分细致的 ...
- 玩转大数据:深入浅出大数据挖掘技术(Apriori算法、Tanagra工具、决策树)
一.本课程是怎么样的一门课程(全面介绍) 1.1.课程的背景 “大数据”作为时下最火热的IT行业的词汇,随之而来的数据仓库.数据分析.数据挖掘等等围绕大数据的商业价值的利用逐渐成为 ...
- Codility上的问题 (16) Omicron 2012
比较无聊的题,求斐波那契数的第N^M项. f(0) = 0, f(1) = 1, f(n) = f(n - 1) + f(n - 2),结果对10000103取模. N, M在[0..10^7]之间. ...
- 有没有安全的工作?(99条评论)——结论是没有一劳永逸的工作,要终身学习,IT业刚出道和老手还是有区别的(同样对于新技术,薪资可能是个问题)
作者: 阮一峰 日期: 2015年12月15日 如果你经常使用互联网,可能知道有一种东西叫做Flash. 它是一种软件,用来制作网页游戏.动画,以及视频播放器.只要观看网络视频,基本都会用到它. 七八 ...
- ios23-文件上传
1.上传图片 3. // // ios23_uploadViewController.h // ios23-upload // // Created by on 13-6-17. // Co ...
- Swift - 启动时的向导页(新手引导)的制作
在很多iOS产品或者一些应用版本的升级中,新手指导都是一个常用的功能,通过说明页的左右滑动,可以很清晰的展示系统的一些功能特性.制作思路如下: 1,如何检测应用是第一次登陆启动 我们可以使用NSUse ...
- 关于ListCtrol自绘的技巧
一.给控件添加排序功能report风格的list控件很多情况下都需要支持排序功能,而且最好支持按不同列进行排序.CListCtrl的类方法SortItems支持排序功能,但是在排序过程中,两个数据真正 ...
- Mysql 和Oracle rows 区别
mysql> explain select t1.* from t2 ,t1 where t2.id=t1.id and t2.id<3;\ +----+-------------+--- ...
- OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
症状描述: Accelerator keys, such as ARROW keys, are first received by the message pump of the ActiveX co ...