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 ...
随机推荐
- Spring Boot 知识笔记(servlet、监听器、拦截器)
一.通过注解自定义servlet package net.Eleven.demo.servlet; import javax.servlet.ServletException; import java ...
- 每日一问:你了解 Java 虚拟机结构么?
对于从事 C/C++ 程序员开发的小伙伴来说,在内存管理领域非常头疼,因为他们总是需要对每一个 new 操作去写配对的 delete/free 代码.而对于我们 Android 乃至 Java 程序员 ...
- SOA & 微服务
参考文档: https://www.cnblogs.com/renzhitian/p/6853289.htmlhttp://www.jdon.com/soa.htmlhttps://www.ibm.c ...
- java基础 类 & 继承
类 在Java中,类文件是以.java为后缀的代码文件,在每个类文件中可以有多个类,但是最多只允许出现一个public类,当有public类的时候,类文件的名称必须和public类的名称相同,若不存在 ...
- log4j2使用RollingFile重启丢失日志问题
<RollingFile name="cnkiLogRollingFileError" fileName="${logbigdata.dir}/Log8080/er ...
- cad.net 图元延迟显示,动画效果,编辑器延迟发送提示.
public class Command_test { [CommandMethod("tt", CommandFlags.Modal | CommandFlags.UsePick ...
- c# winform button文字偏了
winform button文字偏了,解决方案来自 疯狂青蛙: http://www.cnblogs.com/cadlife 要用这个属性
- iOS 测试 WebDriverAgent 简介
WebDriverAgent 是什么 去年的 SeleniumConf 上,Facebook 推出了一款新的iOS移动测试框架 —— WebDriverAgent,当时的推文上,写的还只支持模拟器 ...
- ubuntu16安装python3
正常情况下,你安装好ubuntu16.04版本之后,系统会自带 python2.7版本,如果需要下载新版本的python3.5,就需要进行更新.下面给出具体教程: 1.首先在ubuntu的终端tern ...
- react 父组件调用子组件方法、子组件调用父组件方法
我们闲话不多说,直接上代码 // 父组件 import React, {Component} from 'react'; class Parents extends Component { const ...