kuangbin专题专题四 Til the Cows Come Home POJ - 2387
题目链接: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的更多相关文章
- Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32824 Accepted: 11098 Description Bes ...
- (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...
- 「kuangbin带你飞」专题十四 数论基础
layout: post title: 「kuangbin带你飞」专题十四 数论基础 author: "luowentaoaa" catalog: true tags: mathj ...
- 开发指南专题十四:JEECG微云高速开发平台MiniDao 介绍
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhangdaiscott/article/details/27068645 开发指南专题十四:J ...
- 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 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- Til the Cows Come Home(最短路)
Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
随机推荐
- java web开发入门汇总
servlet 用java语言开发动态资源网站的技术,在doGet方法中拼接显示html,在doPost方法中提交数据.类似于.net的ashx技术. servlet生成的class文件存放在tomc ...
- laravel代码规范强制检查
目录 介绍 代码规范检查与修复 在git commit时自动检查代码规范 后记 介绍 在团队协作开发中,代码规范是必要的.以前的规范都是自己定,然后手动检查,很难做到有效的约束. 现代的PHP,则有得 ...
- Elasticsearch SQL用法详解
Elasticsearch SQL用法详解 mp.weixin.qq.com 本文详细介绍了不同版本中Elasticsearch SQL的使用方法,总结了实际中常用的方法和操作,并给出了几个具体例子 ...
- Python 3.X 练习集100题 03
一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? import math for i in range(10000): n1 = math.sqrt( ...
- jquery插件实现瀑布流
jquery插件实现瀑布流<!DOCTYPE html><html lang="en"><head> <meta charset=&quo ...
- phpcms发布文章自定义字段
phpcms发布文章自定义字段1 进入后台 模型管理 新建模型 里面可以自定义字段了
- putty常用配置修改
1.修改putty默认的颜色方案 初次使用putty时,发现默认的配色的方案看得非常难受,特别是黑色背景,深蓝色的字体,根本看不清楚,下面介绍如何更改默认的配色方案: (1)下载配置文件 首先下载注册 ...
- [转帖]从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑?
从 SOA 到微服务,企业分布式应用架构在云原生时代如何重塑? 2019-10-08 10:26:28 阿里云云栖社区 阅读数 54 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权 ...
- for循环中的switch的break和continue作用范围
for循环中的switch的break和continue作用范围 不空泛的讲理论了,上代码.看下面这个代码: #include <stdio.h> #include <stdlib. ...
- Prometheus 配置采集目标
Prometheus 配置采集目标 1.根据配置的任务(job)以http/s周期性的收刮(scrape/pull)2.指定目标(target)上的指标(metric).目标(target)3.可以以 ...