题意:

有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)的更多相关文章

  1. 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 ...

  2. poj 2387 Til the Cows Come Home(dijkstra算法)

    题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...

  3. POJ 2387 Til the Cows Come Home (Dijkstra)

    传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...

  4. Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)

    题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...

  5. 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 ...

  6. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  7. 怒学三算法 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 ...

  8. 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 ...

  9. 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)

    Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...

随机推荐

  1. 从零开始,将ASP.NET Core部署到Linux生产环境

    研究.NET Core已经一段时间了,一直都是在Windows上开发,这2天尝试着将公司一个很简单的内部Web项目改造成了ASP.NET Core,并且部署到Linux上.生产环境如下: Linux ...

  2. 我的 GitHub 100 连击

    终于达成 gayhub 的第一个100连击了,感觉自己整个人颜色都不一样了,完全蜕变了. PS: GitHub 汉化插件 52cik/github-hans 感兴趣的赶紧 get 起来吧. 遇到瓶颈 ...

  3. 如何解决Windows 10系统下设备的声音问题

    如何解决Windows 10系统下设备的声音问题? 请阅读下面的说明来解决Windows 10设备上的声音问题. 1. 检查设备管理器 打开开始菜单,键入设备管理器, 从出现的结果中选择并打开它. 在 ...

  4. P值与significant(显著性)的理解

    P值与significant的理解 来源:广州市统计局   发表日期:2015-01-21     P值可以理解为结论的风险大小,也就是根据数据得出的结果有多大的错误风险,P值越小,结论错误的风险越小 ...

  5. Congruence relation 同余关系

    https://en.wikipedia.org/wiki/Congruence_relation https://zh.wikipedia.org/wiki/%E5%90%8C%E9%A4%98%E ...

  6. 品牌OEM信息导入工具(实测支持Win10)

    OEM修改,定制专属LOGO. 免费下载:http://yunpan.cn/cmZuTYWLIGX6Q  访问密码 2da7 备用通道:            http://pan.baidu.com ...

  7. IntelliJ idea的使用

    1.快捷键 2.插件集成 附录:参考资料

  8. Thinkphp url 除去index.php

    例如你的原路径是 http://localhost/test/index.php/index/add那么现在的地址是 http://localhost/test/index/add如何去掉index. ...

  9. tomcat 监控

    主要监控JVM (1)JPS(快速获取Java的PID) 这个是jdk包里的 显示Java进程的 +l +v +m (2)jstack 命令格式: 常用说明: (3)jmap 现在列举生产中遇到的JV ...

  10. 网页中常用HTML字符实体

    摘要: 一些字符在 HTML 中拥有特殊的含义,比如小于号 () 用于定义 HTML 标签的开始.如果我们希望浏览器正确地显示这些字符,我们必须在 HTML 源码中插入字符实体. 字符实体有三部分:一 ...