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环境: shell初始化文件, for TCSH, CSH
TCSHELL, CSHELL 配置文件 全局配置文件 /etc/csh.cshrc个人配置文件 ~/.cshrc或~/.tcshrc 参考: 1.配置你的csh/tcsh, https://wik ...
- 用Delphi进行word开发
使用以CreateOleObjects方式调用Word 实际上还是Ole,但是这种方式能够真正做到完全控制Word文件,能够使用Word的所有属性,包括自己编写的VBA宏代码.------------ ...
- Swift - 获取屏幕点击坐标下所有对象(SpriteKit游戏开发)
对于场景内对象元件的点击响应,我们可以在场景的touchesBegan()方法中内统一处理. SKScene中touchesBegan()是响应屏幕点击的方法,在这里面我们可以先获取点击位置下所有的对 ...
- HDU 3584 三维树状数组
三维树状数组模版.优化不动了. #include <set> #include <map> #include <stack> #include <cmath& ...
- linux安装Tesseract-OCR
安装Tesseract-OCR 1. leptonica 需要源码编译安装http://www.leptonica.org/ leptonica 包: leptonica-1.73.tar.gz 解 ...
- 手机字段存储报错 :Warning Code : 1264 Out of range value for column 'buyer_tpl' at row 1
企鹅上朋友问我: 我这明明是11位的int 为啥还说超出范围了呢,然后发来报警截图 我看到是 buyer_tpl int(13) unsigned NOT NULL,就知道是怎么回事了,打开dev. ...
- 汇编与高级语言(插图结合Delphi代码,来自linzhengqun)
汇编与高级语言 1. 汇编基础知识 1.1. 寄存器 寄存器 用途 EAX,EBX,EDX,ECX 通用寄存器,由程序员自己指定用途,也有一些不成文的用法: EAX:常用于运算. ...
- URAL 1963 Kite 四边形求对称轴数
题目链接: http://acm.timus.ru/problem.aspx?space=1&num=1963 题意,顺时针或逆时针给定4个坐标,问对称轴有几条,输出(对称轴数*2) 对于一条 ...
- poj 1611 The Suspects(并查集)
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 21598 Accepted: 10461 De ...
- delphi 对抗任务管理器关闭(提升进程到Debug模式,然后设置进程信息SE_PROC_INFO)
[delphi] view plain copy program Project1; uses Windows; {$R *.res} function MakeMeCritical(Yes: Boo ...