以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 0x3f3f3f3f int 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 0x3f3f3f3f int 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 0x3f3f3f3f int 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 0x3f3f3f3f int 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;
}

ACM/ICPC 之 Floyd练习六道(ZOJ2027-POJ2253-POJ2472-POJ1125-POJ1603-POJ2607)的更多相关文章

  1. ACM/ICPC 之 Floyd范例两道(POJ2570-POJ2263)

    两道以Floyd算法为解法的范例,第二题如果数据量较大,须采用其他解法 POJ2570-Fiber Network //经典的传递闭包问题,由于只有26个公司可以采用二进制存储 //Time:141M ...

  2. ACM/ICPC 之 Floyd+记录路径后继(Hdu1385(ZOJ1456))

    需要处理好字典序最小的路径 HDU1385(ZOJ1456)-Minimum Transport //Hdu1385-ZOJ1456 //给定邻接矩阵,求给定起点到终点的最短路径,若有相同路长的路径按 ...

  3. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  4. ACM/ICPC 之 BFS(离线)+康拓展开(TSH OJ-玩具(Toy))

    祝大家新年快乐,相信在新的一年里一定有我们自己的梦! 这是一个简化的魔板问题,只需输出步骤即可. 玩具(Toy) 描述 ZC神最擅长逻辑推理,一日,他给大家讲述起自己儿时的数字玩具. 该玩具酷似魔方, ...

  5. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 G. Garden Gathering

    Problem G. Garden Gathering Input file: standard input Output file: standard output Time limit: 3 se ...

  6. ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 D. Delay Time

    Problem D. Delay Time Input file: standard input Output file: standard output Time limit: 1 second M ...

  7. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  8. 【转】lonekight@xmu·ACM/ICPC 回忆录

    转自:http://hi.baidu.com/ordeder/item/2a342a7fe7cb9e336dc37c89 2009年09月06日 星期日 21:55 初识ACM最早听说ACM/ICPC ...

  9. 【转】ACM/ICPC生涯总结暨退役宣言—alpc55

    转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...

随机推荐

  1. motto2

    Baby you've done enough that cut your breath.Don't beat yourself up don't need to turn so fast.Somet ...

  2. c# 中使用memcached

    1.首先下载memcached 服务端 2.使用Enyim.Caching .Net 客户端 3.配置web.config    <sectionGroup name="QuickBo ...

  3. Linux里startup.sh 和 shutdown.sh

    最近用socket编写了一个服务端程序,监听1024端口,检测客户端发来的请求,所在Linux里写启动和停止的脚本: 在Eclipse里java写好程序,右击导出生成 Runnable JAR fil ...

  4. php从零开始

    吐槽:今天开始撸PHP了,从此前端少了个小白,PHP多了个小白... 本白从3年前陆陆续续开始一会儿撸会儿PHP一会儿撸前端.前端撸的比较多,PHP撸的比较少,当然本白撸php大多都是被逼的!! 然后 ...

  5. html5手机常见问题与工具分享

    mobileTech A useful tools or tips list for mobile web application developing 这个项目收集移动端开发所需要的一些资源与小技巧 ...

  6. log4net--不可多得的开源日志记录组件

    log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...

  7. iOS开发关于Block代码错误

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...

  8. [Storm] 内部消息缓存

    这篇文件翻译自 http://www.michael-noll.com/blog/2013/06/21/understanding-storm-internal-message-buffers/ 当进 ...

  9. 兼容ie的jquery ajax文件上传

    Ajax文件上传插件很多,但兼容性各不一样,许多是对ie不兼容的,另外项目中是要求将网页内容嵌入到桌面端应用的,这样就不允许带flash的上传插件了,如:jquery uploadify...悲剧 对 ...

  10. 高性能滚动 scroll 及页面渲染优化

    最近在研究页面渲染及web动画的性能问题,以及拜读<CSS SECRET>(CSS揭秘)这本大作. 本文主要想谈谈页面优化之滚动优化. 主要内容包括了为何需要优化滚动事件,滚动与页面渲染的 ...