题目链接:https://vjudge.net/problem/POJ-2387

题意:从编号为n的城市到编号为1的城市的最短路。

思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者看。

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <string>
using namespace std; typedef long long LL;
#define inf (1LL << 30) - 1
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--) const int N = ;
bool vis[N]; //是否访问过
int mp[N][N];
int dis[N]; //到不同城市的距离
int t,n; void init(){
memset(vis,,sizeof(vis));
rep(i,,n) rep(j,,n){
if(i == j) mp[i][j] = ;
else mp[i][j] = inf;
}
} void input(){ int u,v,w;
rep(i,,t){
cin >> u >> v >> w;
if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
} } void dijkstra(){ rep(i,,n) dis[i] = mp[n][i]; //n到其他城市的距离
vis[n] = true; //标记n城市任务完成 rep(i,,n){ //接下来 n-1个城市的操作 int x = -;
int c = inf; rep(j,,n){
//该城市未被访问过 选出当前到每个点的最小值,并得到坐标
if(!vis[j] && c > dis[j]) x = j, c = dis[j];
}
if(x == -) continue; //没找到一个,即该城市无法到达其他未被访问的城市 vis[x] = true; //标记这个当前这个离起始点最短距离的城市
rep(p,,n){ //起始点到p城市的所有距离之和 大于 起始点到x点的所有距离之后 加上 x点到p点的距离
if(!vis[p] && dis[x] + mp[x][p] < dis[p]){
dis[p] = dis[x] + mp[x][p];
}
} }
//到达点1城市的最短距离
cout << dis[] << endl;
} int main(){ ios::sync_with_stdio(false);
cin.tie(); cin >> t >> n; init(); //初始化
input(); //输入
dijkstra(); //最短路 getchar();getchar(); return ;
}

kuangbin专题专题四 Til the Cows Come Home POJ - 2387的更多相关文章

  1. Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32824   Accepted: 11098 Description Bes ...

  2. (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387

      #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...

  3. 「kuangbin带你飞」专题十四 数论基础

    layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...

  4. 开发指南专题十四:JEECG微云高速开发平台MiniDao 介绍

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/27068645   开发指南专题十四:J ...

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

  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. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  8. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  9. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

随机推荐

  1. [Noip2018]填数游戏

    传送门 Description 耳熟能详,就不多说了 Solution 对于一个不会推式子的蒟蒻,如何在考场优雅地通过此题 手玩样例,发现对于 \(n=1\) , \(ans=2^m\) .对于 \( ...

  2. 【HDU6216】 A Cubic number and A Cubic Number 和 广工的加强版

    题目传送门_杭电版 题目传送门_广工版 广工版的是杭电版的加强版. 题意:判断一个质数是否是两个整数的立方差 ---- 数学题 题解: 根据立方差公式:\(a^3 - b^3 = (a - b)(a^ ...

  3. 对C++11中的`移动语义`与`右值引用`的介绍与讨论

    本文主要介绍了C++11中的移动语义与右值引用, 并且对其中的一些坑做了深入的讨论. 在正式介绍这部分内容之前, 我们先介绍一下rule of three/five原则, 与copy-and-swap ...

  4. Mac-搭建Hadoop集群

    You have to work very hard to believe that you are really powerless. Mac-搭建Hadoop集群 我用到了:VMware Fusi ...

  5. [BUAA软工]团队贡献分博客

    Gamma阶段贡献分 Beta阶段贡献分 Alpha阶段贡献分 (博客最后部分) 复制过来: Alpha阶段 名字 角色 具体的可衡量的可验证的贡献 zpj PM,后端开发 博客X3 65 commi ...

  6. stacking method house price in kaggle top10%

    整合几部分代码的汇总 隐藏代码片段 导入python数据和可视化包 导入统计相关的工具 导入回归相关的算法 导入数据预处理相关的方法 导入模型调参相关的包 读取数据 特征工程 缺失值 类别特征处理-l ...

  7. 快速排序详解(C语言/python)

    快速排序详解 介绍: 快速排序于C. A. R. Hoare在1960年提出,是针对冒泡排序的一种改进.它每一次将需要排序的部分划分为俩个独立的部分,其中一个部分的数比的数都小.然后再按照这个方法对这 ...

  8. CentOS 7.6 安装Python3.7.2 多版本共存

    CentOS 7.6 默认安装了 Python 2.7.5 准备环境 yum install git gcc gcc-c++ make automake autoconf libtool pcre p ...

  9. 适合 ASP.NET Core 的超级-DRY开发

    作者 Thomas Hansen DRY 是那些非常重要的软件体系结构缩写之一.它的意思是“不要自我重复”,并向维护旧源代码项目的任何用户阐明了一个重要原则.也就是说,如果你在代码中自我重复,会发现每 ...

  10. 把jar包安装到本地Maven仓库

    使用的场景 自己写的工具类想安装到本地 从Maven仓库中下载不下来的jar 使用的步骤       首先要保证自己的Maven配置全局环境变量,如果没有配置过maven全局变量,可以按照下面的步骤配 ...