题目链接: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. 写代码注意了,打死都不要用 User 这个单词

    阅读本文大概需要 4 分钟. 原文:http://t.cn/Eau2d0h 译文:http://21cto.com/article/2093 当你意识到你在项目开始时做的轻量.简单的设想竟然完全错了时 ...

  2. Thrift源码分析(二)-- 协议和编解码

    协议和编解码是一个网络应用程序的核心问题之一,客户端和服务器通过约定的协议来传输消息(数据),通过特定的格式来编解码字节流,并转化成业务消息,提供给上层框架调用. Thrift的协议比较简单,它把协议 ...

  3. IE 浏览器设置 打开新的选项卡而不是弹出窗口

    首先打开IE的页面  找到工具 —点击Internet选项

  4. 【06月04日】A股滚动市盈率PE历史新低排名

    2010年01月01日 到 2019年06月04日 之间,滚动市盈率历史新低排名. 上市三年以上的公司,2019年06月04日市盈率在300以下的公司. 1 - 阳光照明(SH600261) - 历史 ...

  5. Android studio中遇到的问题

    首先声明只是Android studio使用中遇到的问题纯属个人学习笔记,有什么不对的可以留言. 将脱壳后的java文件拖入到Android studio android studio 首先提示是ER ...

  6. Laravel集成Swoole教程

    1.准备工作 安装 Laravel laravel new laravel-swoole     本人使用 valet 进行开发,可以使用 laravel-swoole.test     进行访问 2 ...

  7. 腾讯云IPv6技术拿了个一等奖!1.5亿人已经用上

    中国通信学会在其官网上公布了2019年中国通信学会科学技术奖的评选结果,腾讯云和中国移动通信集团,中国信息通信研究院.以及华为联合申报的“移动互联网IPv6技术攻关及规模应用”项目荣获今年科学技术一等 ...

  8. [转帖]iostat 命令详解

    iostat 命令详解 https://www.jianshu.com/p/5fed8be1b6e8 概述 iostat 主要用于输出磁盘IO 和 CPU的统计信息. iostat属于sysstat软 ...

  9. 递推 + 高精度 --- Tiling

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7264   Accepted: 3528 Descriptio ...

  10. k8s-Label(标签)

    k8s-Label(标签) 一.Label是什么? Label是Kubernetes系统中的一个核心概念.Label以key/value键值对的形式附加到各种对象上,如Pod.Service.RC.N ...