POJ - Til the Cows Come Home(Dijkstra)
题意:
有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离
分析:
典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板。
题解
#include<iostream>
#include<Queue>
#include<cstdio>
#include<vector>
#include<string.h>
using namespace std;
#define maxn 2001
#define inf 0x3f3f3f
int map[maxn][maxn];
int dist[maxn];
bool visited[maxn];
typedef pair<int,int> P;
void dijkstra(int s,int n){
priority_queue<P,vector<P>,greater<P> > Q;
memset(visited,0,sizeof(visited));
for(int i=1;i<=n;i++){
dist[i]=inf;
}
dist[s]=0;
Q.push(P(0,s)); while(!Q.empty()){
int v=Q.top().second;
Q.pop();
if(visited[v]) continue;
visited[v]=true;
for(int i=1;i<=n;i++){
int len=dist[v]+map[v][i];
if(dist[i]>len){
dist[i]=len;
Q.push(P(len,i));
}
}
}
}
int main(){
int n,t;
while(scanf("%d %d",&t,&n)!=EOF){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
map[i][j]=(i==j?0:inf);
}
}
while(t--){
int b,c,d;
scanf("%d %d %d",&b,&c,&d);
if(map[b][c]>d){
map[b][c]=d;
map[c][b]=d;
}
}
dijkstra(n,n);
printf("%d\n",dist[1]);
}
}
POJ - Til the Cows Come Home(Dijkstra)的更多相关文章
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- Til the Cows Come Home (dijkstra算法)
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)
Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...
随机推荐
- JVM内存管理------杂谈(借此也论一论obj=null)
各位园友好,LZ是从某网站转战过来的博主,看到这里很多博主都称看友们为园友,LZ斗胆模仿一下,不过以前,LZ其实都是称看友们为猿友的.之前LZ在某网站已经写了一系列文章,已经全部复制到了园内的新博客, ...
- DB2和Oracle区别
转 http://blog.chinaunix.net/uid-7374279-id-2057574.html 写在前面:今天客户来访(日本人),问我DB2和Oracle区别.因为不是DBA(勉强的理 ...
- Bete冲刺第四阶段
Bete冲刺第四阶段 今日工作: web: 昨晚搞得很晚,帮队友搞定了git的问题,仓库顿时干净多了,同时已经基本完成了基础功能的接口 ios: 导入并使用了改善交互的第三方开源库,修正路径BUG 目 ...
- 1014mysqldumpslow.pl简单分析慢日志 WINDOW平台
转自http://www.th7.cn/db/mysql/201507/113998.shtml 要想运行mysqldumpslow.pl(这是perl程序),下载perl编译器.下载地址:http: ...
- 如何使用国内源部署Ceph?
由于网络方面的原因,Ceph的部署经常受到干扰,通常为了加速部署,基本上大家都是将Ceph的源同步到本地进行安装.根据Ceph中国社区的统计,当前已经有国内的网站定期将Ceph安装源同步,极大的方便了 ...
- OAuth in One Picture
近年来,OAuth在各种开放平台的引领下变得非常流行,上图是OAuth协议认证的全过程,图本身已经比较详细,这里不再赘述. 从上图中可以看出,OAuth协议中有三个角色: User, Consumer ...
- Ubuntu14.04下jdk的安装
1.下载JDK目前最新的JDK版本是:Java SE Development Kit 8u52.解压安装我们把JDK安装到这个路径:/usr/lib/jvm如果没有这个目录(第一次当然没有),我们就新 ...
- editplus-查找替换的正则表达式应用
editplus查找替换的正则表达式应用 表达式 说明 \t 制表符. \n 新行. . 匹配任意字符. | 匹配表达式左边和右边的字符. 例如, "ab|bc" 匹配 " ...
- 华硕笔记本BIOS设置详解
BIOS是英文Basic Input/Output System的缩写,即基本输入输出系统,合理的BIOS设置可以让我们的电脑更好的为我们服务,但由于大部分笔记本的BIOS界面是英文,并且BIOS里面 ...
- cookie的写入与读出
cookie在jquery中有指定的cookie操作类 $.cookie('the_cookie'); // 读取 cookie $.cookie('the_cookie', 'the_value') ...