题目链接: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. Maven 基本概念——根目录、项目创建、坐标

     1. MavenProjectRoot(项目根目录)   |----src   |     |----main   |     |         |----java ——存放项目的.java文件  ...

  2. Spring Boot进阶系列四

    这边文章主要实战如何使用Mybatis以及整合Redis缓存,数据第一次读取从数据库,后续的访问则从缓存中读取数据. 1.0 Mybatis MyBatis 是支持定制化 SQL.存储过程以及高级映射 ...

  3. spring学习-ApplicationContext-spring上下文深入理解

    4月份开始复习一遍spring相关知识.让自己巩固一下spring大法的深奥益处,所以就看了大佬的博客,转载留下来日后继续研读.认为重点的标记为红色 以下文章内容转载自:http://www.cnbl ...

  4. Spring Security教程之整合SpringMVC(六)

    一.前言 Spring Security系列教程中,前五篇为同一人所写,而本文是博主依据第三方文章整合而出,与前五篇文章的作者不是同一系列. 但本文以前五篇文章为基础,在前面文章所建立的Spring ...

  5. c++中如何判断sqlite表是否存在

    在项目中遇到需要判断sqlite数据库中某个表是否存在,上网搜索一些资料后,解决了问题,如下: 首先,在每个sqlite数据库中,都有一个名为sqlite_master的表,它定义了数据库的模式,它的 ...

  6. [Atcoder AGC032C]Three Circuits

    题目大意:有一张$n$个点$m$条边的无向连通图,判断是否可以从中分出$3$个环,满足三个环覆盖整张图并且没有重复的边.$n,m\leqslant10^5$ 题解:分类讨论.有度数为奇肯定不行,因为连 ...

  7. HTML 引用大全

    路径logo <link rel="icon" href="../framework7-4.4.10/kitchen-sink/core/img/ztjs.png& ...

  8. Web前端推荐学习站点

    http://javascript.ruanyifeng.com/   JavaScript参考标准教程,写的很不错. https://www.xiaohuochai.cc/  小火柴前端站 http ...

  9. Socket心跳机制-JS+PHP实现

    本文是我在实际工作中用到的Socket通信,关于心跳机制的维护方式,特意总结了一下,希望对朋友们有所帮助. Socket应用:首先Socket 封装了tcp协议的,通过长连接的方式来与服务器通信,是由 ...

  10. comet oj #7

    A 签到题 题目描述 多次询问,每次询问给一个值域范围 [l,r][l,r],要回答下列四个问题: 从这个范围内选出两个整数(两个数可相同), (1) 这两个数的最小公倍数最大是多少? (2) 这两个 ...