ACM/ICPC 之 Floyd练习六道(ZOJ2027-POJ2253-POJ2472-POJ1125-POJ1603-POJ2607)

以Floyd解法为主的练习题六道

 


ZOJ2027-Travelling Fee

 

//可免去一条线路中直接连接两城市的最大旅行费用,求最小总旅行费用
//Time:0Ms	Memory:604K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;#define MAX 205
#define MAXS 12
#define INF 0x3f3f3f3fint n,m;
int dis[MAX][MAX];
int high[MAX][MAX];	//该线路中直接连接两城市的最大旅行费用struct City {char name[MAXS];
}c[MAX];int find(char s[MAXS])
{for (int i = 0; i < n; i++)if (!strcmp(s, c[i].name))	return i;return -1;
}void floyd()
{for (int k = 0; k < n; k++)for (int i = 0; i < n; i++)for (int j = 0; j < n; j++){if (high[i][k] == -1 || high[k][j] == -1)	continue;int maxCost = max(high[i][k], high[k][j]);if (dis[i][j] - high[i][j] > dis[i][k] + dis[k][j] - maxCost){dis[i][j] = dis[i][k] + dis[k][j];high[i][j] = maxCost;}}
}int main()
{while (scanf("%s%s", c[0].name, c[1].name) != EOF){memset(dis, INF, sizeof(dis));memset(high, -1, sizeof(high));int d; n = 2;char s[MAXS],t[MAXS];scanf("%d", &m);while (m--){scanf("%s%s%d", s, t, &d);int ns = find(s), nt = find(t);if (ns == -1) {memcpy(c[n].name, s, sizeof(s));ns = n++;}if (nt == -1) {memcpy(c[n].name, t, sizeof(t));nt = n++;}high[ns][nt] = dis[ns][nt] = d;}floyd();printf("%d\n", dis[0][1] - high[0][1]);}return 0;
}

 

 


 

POJ2253-Frogger

 

 

//求0->1间的最小的最大单次青蛙跳跃距离
//Time:94Ms	Memory:508K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;#define MAX 205
#define POW2(x) ((x)*(x))
#define DIS(a,b) (sqrt(POW2(p[a].x - p[b].x) + POW2(p[a].y - p[b].y)))struct Point {double x, y;
}p[MAX];int n;
double dis[MAX][MAX];	//两点间最小的最大一次跳跃距离void floyd()
{for (int k = 0; k < n; k++)for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)dis[i][j] = min(dis[i][j], max(dis[i][k], dis[k][j]));
}int main()
{int cas = 1;while (scanf("%d", &n), n){for (int i = 0; i < n; i++){scanf("%lf%lf", &p[i].x, &p[i].y);for (int j = 0; j < i; j++)dis[i][j] = dis[j][i] = DIS(i, j);dis[i][i] = 0;}floyd();printf("Scenario #%d\n", cas++);printf("Frog Distance = %.3f\n\n", dis[0][1]);}return 0;
}

 

 


 

POJ2472-106 miles to Chicago

 

 

//求1到n的最大存活率
//Time:157Ms	Memory:264K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;#define MAX 105
int n, m;
double dis[MAX][MAX];void floyd()
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)dis[i][j] = max(dis[i][j], dis[i][k] * dis[k][j]);
}int main()
{while (scanf("%d%d", &n, &m), n){memset(dis, 0, sizeof(dis));int x, y, d;while (m--) {scanf("%d%d%d", &x, &y, &d);dis[x][y] = dis[y][x] = d / 100.0;}floyd();printf("%.6lf percent\n", 100*dis[1][n]);}return 0;
}

 

 


 

POJ1125-Stockbroker Grapevine

 

 

//从某点扩散到所有点的最短时间-求出该点及时间
//Time:0Ms	Memory:208K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;#define MAX 105
#define INF 0x3f3f3f3fint n, m;
int dis[MAX][MAX];
int time[MAX];void floyd()
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}int main()
{while (scanf("%d", &n), n){memset(dis, INF, sizeof(dis));memset(time, 0, sizeof(time));int y, d;for (int x = 1; x <= n; x++){scanf("%d", &m);while (m--) {scanf("%d%d", &y, &d);dis[x][y] = d;dis[x][x] = 0;}}floyd();int Min = INF;int k = 0;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)time[i] = max(time[i], dis[i][j]);for (int i = 1; i <= n; i++){if (Min > time[i]){Min = time[i];k = i;}}if (Min == INF)	printf("disjoint\n");else printf("%d %d\n", k, Min);}return 0;
}

 

 


 

POJ1603-Risk

 

 

//求依靠攻打接壤国需要最少攻打多少国家才能从起始国家打到终止国家
//Time:0Ms	Memory:168K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;#define MAX 21
#define INF 0x3f3f3f3fint n, m;
int dis[MAX][MAX];void floyd()
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}int main()
{int cas = 0;n = 20;while (scanf("%d", &m) != EOF){memset(dis, INF, sizeof(dis));int x, y;for (x = 1; x <= 19; x++){while (m--) {scanf("%d", &y);dis[x][y] = dis[y][x] = 1;}scanf("%d", &m);}floyd();if (cas)	printf("\n");printf("Test Set #%d\n", ++cas);while (m--) {scanf("%d%d", &x, &y);printf("%d to %d: %d\n", x, y, dis[x][y]);}}return 0;
}

 

 


 

POJ2607-Fire Station

  这道题稍微比上面的五道题复杂一些

 

//Floyd解法
//已知消防站,现建新消防站,使得所有路口到消防站的最大值减至最小,将最小序列路口输出
//Time:1797Ms	Memory:1164K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;#define MAX 505
#define INF 0x3f3f3f3fint f, n, m;
int dis[MAX][MAX];
bool v[MAX];
int md[MAX];    //各路口到消防站最短距离void floyd()
{for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}int main()
{memset(dis, INF, sizeof(dis));memset(md, INF, sizeof(md));scanf("%d%d", &f, &n);int x, y, d;for (int i = 0; i < f;i++){scanf("%d", &x);v[x] = true;dis[x][x] = 0;}while (scanf("%d%d%d", &x, &y, &d) != EOF)dis[x][y] = dis[y][x] = d;floyd();for (int i = 1; i <= n; i++){dis[i][i] = 0;if (!v[i])	continue;for (int j = 1; j <= n; j++)md[j] = min(md[j], dis[i][j]);}int Max = INF, k = 1;	//默认为1,否则A不掉(我怀疑数据有问题或题目描述不清)for (int i = 1; i <= n; i++){if (v[i])	continue;int tmp = 0;for (int j = 1; j <= n; j++)tmp = max(tmp, min(md[j], dis[i][j]));if (Max > tmp){Max = tmp;k = i;}}printf("%d\n", k);return 0;
}

 

转载于:https://www.cnblogs.com/Inkblots/p/5499802.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/470656.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Chapter4-2_Speech_Synthesis(More than Tacotron)

文章目录1 Mispronunciation2 More information for Encoder3 Attention4 Fast Speech and DurIAN5 Dual Learning5 Controllable TTSSpeaker EmbeddingGST-TacotronTwo-stage Training本文为李弘毅老师【Speech Synthesis - More than Tacotron】的课程笔记&#xff0c;课程视…

art-template模板引擎详解

1. 模板引擎 art-template中文文档&#xff1a;https://www.kancloud.cn/lanju/art-template/1500276 1.1 Ajax 项目中存在的问题 数据和HTML字符串拼接导致代码混乱&#xff0c;拼接容易出错&#xff0c;增加修改难度。 业务逻辑和用户界面混合&#xff0c;代码不易维护。 …

python3语法糖_Python笔记3:语法糖

运算 数字运算 运算会根据结果自动判断结果是int还是float 用到除法的时候&#xff0c;结果自动输出为float 双斜杠//得到的结果是int 取模(余数)还是% >>> 22 4 >>> 50-5*6 20 >>> (50-5*6)/4 5.0 >>> 8/5 1.6 >>> 5.0/1.6 3.12…

【代码笔记】iOS-清除图片缓存UIActionSheet

一&#xff0c;效果图。 二&#xff0c;代码。 RootViewController.m //点击任何处出现sheet -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {UIActionSheet * sheet [[UIActionSheet alloc] initWithTitle:"确定要清空图片缓存&#xff1f;" d…

Chapter5_Speaker_Verification

文章目录1 Task Introduction2 模型架构3 模型介绍3.1 i-vector3.2 d-vector3.3 x-vector3.4 more4 End to End本文为李弘毅老师【Speaker Verification】的课程笔记&#xff0c;课程视频youtube地址&#xff0c;点这里&#x1f448;(需翻墙)。 下文中用到的图片均来自于李宏毅…

python如何读取字典的关键字_python提取字典key列表的方法

python提取字典key列表的方法 更新时间&#xff1a;2015年07月11日 12:04:48 作者&#xff1a;企鹅不笨 这篇文章主要介绍了python提取字典key列表的方法,涉及Python中keys方法的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了python提取字典key列表的方法…

使用express搭建服务器获取MySQL数据库数据

一、原始的mysql查询方法 先安装mysql cnpm install mysql --save 引入这个db.js之后&#xff0c;才能对数据库进行查询 进行查询 查询结果如下&#xff1a; 二、ORM 介绍 ORM 全拼Object-Relation Mapping. 中文意为 对象-关系映射. 主要实现模型对象到关系数据库…

java GZIP压缩和解压

最近碰到了一个按GZIP解压指定的输入流数据&#xff0c;备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream;/*** 压缩&#xff…

python数据库连接池_Python实现数据库连接池

1.初始化 def __init__(self, **kwargs): self.size kwargs.get(size, 10) self.kwargs kwargs self.conn_queue queue.Queue(maxsizeself.size) for i in range(self.size): self.conn_queue.put(self._create_new_conn()) size&#xff1a;连接池支持的连接数&#xff0c;…

Chapter6_Vocoder

文章目录1 Introduction2 WaveNet2.1 WaveNet的架构2.2 Softmax Distribution2.3 Causal Convolution和Dilated Convolution2.4 Gated Activation Unit2.5 小结3 FFTNet4 WaveRNN4.1 Dual Softmax Layer4.2 Model Coarse4.3 Model Fine4.4 小结5 WaveGlow本文为李弘毅老师【Voc…

show一下自己的文档编写功底

以我为例&#xff0c;我绝对相信&#xff0c;“才华”和颜值成反比。“才华”二字加了引号了&#xff0c;自知跟优秀有孙大圣一个筋斗云的距离&#xff0c;不过某些细节方面表现得被认为还不错&#xff0c;这里我要秀一下我的文档编写能力。在我这十年的工作生涯里&#xff0c;…

mysql命令速查手册

数据准备 -- 创建数据库 create database qianduan_test charsetutf8;-- 使用数据库 use qianduan_test;-- students表 create table students(id int unsigned primary key auto_increment not null,name varchar(20) default ,age tinyint unsigned default 0,height decima…

Chapter7-1_Overview of NLP Tasks

文章目录1 Introduction2 Part-of-Speech(POS) Tagging3 Word Segmentation4 Parsing5 Coreference Resolution6 Summarization7 Machine Translation8 Grammar Error Correction9 Sentiment classification10 Stance Detection11 Natural Language Inference(NLI)12 Search En…

python制作文本编辑器_Python小实战:制作文本编辑器

学了半年了&#xff0c;该施展一下了&#x1f37a; 做什么呢&#xff1f;做一个简单的文本编辑器吧 来&#xff0c;开始 知识点&#xff1a; 1&#xff09;做窗体的知识 2&#xff09;文件操作 窗体用的是tkinter简单模块&#xff0c;系统自带模块 有人说了&#xff1a;“哇&am…

OGNL 详解

Struts2 OGNL详解 1.概念&#xff1a; OGNL是Object-Graph Navigation Language的缩写&#xff0c;全称为对象图导航语言&#xff0c;是一种功能强大的表达式语言&#xff0c;它通过简单一致的语法&#xff0c;可以任意存取对象的属性或者调用对象的方法&#xff0c;能够遍历整…

完善获取数据库数据的写法

上一篇&#xff1a;使用express搭建服务器获取MySQL数据库数据 一、完善获取数据库数据的写法 asyncawait版本&#xff1a; const express require(express) const db require(./db/nodejs-orm/index.js)const app express()app.get("/get_data", (req, res) …

Chapter7-2_BERT and its family - Introduction and Fine-tune

文章目录1 What is pre-train model2 How to fine-tune2.1 Input2.2 Output2.3 Fine-tune2.4 Weighted Features3 Why fine-tune本文为李弘毅老师【BERT and its family - Introduction and Fine-tune】的课程笔记&#xff0c;课程视频youtube地址&#xff0c;点这里&#x1f4…

python数据分析基础教程 numpy_Python数据分析基础教程:NumPy学习指南(第2版)

Python数据分析基础教程&#xff1a;NumPy学习指南&#xff08;第2版&#xff09; Ivan Idris (作者) 张驭宇 (译者) NumPy是一个优秀的科学计算库&#xff0c;提供了很多实用的数学函数、强大的多维数组对象和优异的计算性能&#xff0c;不仅可以取代Matlab和Mathematica的许多…

【BZOJ-2435】道路修建 (树形DP?)DFS

2435: [Noi2011]道路修建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3115 Solved: 1002[Submit][Status][Discuss]Description 在 W 星球上有 n 个国家。为了各自国家的经济发展&#xff0c;他们决定在各个国家之间建设双向道路使得国家之间连通。但是每个国家的国王都…

CSRF跨站请求伪造

一、CSRF跨站请求伪造 CSRF全拼为Cross Site Request Forgery&#xff0c;译为跨站请求伪造。 CSRF指攻击者盗用了你的身份&#xff0c;以你的名义发送恶意请求。 包括&#xff1a;以你名义发送邮件&#xff0c;发消息&#xff0c;盗取你的账号&#xff0c;甚至于购买商品&a…