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 ...
随机推荐
- [Noip2018]填数游戏
传送门 Description 耳熟能详,就不多说了 Solution 对于一个不会推式子的蒟蒻,如何在考场优雅地通过此题 手玩样例,发现对于 \(n=1\) , \(ans=2^m\) .对于 \( ...
- python爬虫实例——爬取歌单
学习自<<从零开始学python网络爬虫>> 爬取酷狗歌单,保存入csv文件 直接上源代码:(含注释) import requests #用于请求网页获取网页数据 from b ...
- 第01组 Alpha冲刺(6/6)
队名:007 组长博客: https://www.cnblogs.com/Linrrui/p/11914337.html 作业博客: https://edu.cnblogs.com/campus/fz ...
- Computer-Hunters——凡事预则立
Computer-Hunters--凡事预则立 冲刺时间安排 时间 前端计划完成工作 11.6 完成用户注册登录界面,个人信息界面,以及热点资讯界面 11.7-11.8 匹配界面,电脑猎场界面 11. ...
- JVM系列之一:JVM架构
1.简介 Java平台可分为两部分,即Java虚拟机(Java virtual machine,JVM)和Java API类库. JVM是Java Virtual Machine(Java虚拟机)的缩 ...
- Linux配置DNS
vi /etc/resolv.conf, 后面加上nameserver 114.114.114.114
- PatchMatch Stereo - Stereo Matching with Slanted Support Windows
Tips MVS: Multi-View Stereo Abstract 思路:一般的局部立体方法是一个具有整形数值视察(disparity)的支持镜头进行匹配,其中有一个隐藏的假设:再支持区域的像素 ...
- Thinkphp中的系统变量
Action中使用的系统常量 THINK_PATH // ThinkPHP 系统目录 APP_PATH // 当前项目目录 APP_NAME // 当前项目名称 MODULE_NAME //当前模块名 ...
- [转帖]Linux操作系统定时任务系统 Cron 入门0
Linux操作系统定时任务系统 Cron 入门 https://www.cnblogs.com/zhuiluoyu/p/5646400.html cron是一个linux下的定时执行工具,可以在无 ...
- jboss/wildfly安全域的密码加密和解密
加密: java_path=$(source /opt/wildfly/bin/.Beta1.jar:/opt/wildfly/modules/system/layers/base/org/jboss ...