usaco2.4.3 Cow Tours
来源:互联网 发布:淘宝买家诈骗怎么办 编辑:程序博客网 时间:2024/06/06 19:29
一 原题
Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pastures that cannot be connected by any sequence of cow paths, thus partitioning Farmer John's farm into multiple fields.
Farmer John would like add a single a cow path between one pair of pastures using the constraints below.
A field's `diameter' is defined to be the largest distance of all the shortest walks between any pair of pastures in the field. Consider the field below with five pastures, located at the points shown, and cow paths marked by lines:
15,15 20,15 D E *-------* | _/| | _/ | | _/ | |/ | *--------*-------* A B C 10,10 15,10 20,10
The `diameter' of this field is approximately 12.07106, since the longest of the set of shortest paths between pairs of pastures is the path from A to E (which includes the point set {A,B,E}). No other pair of pastures in this field is farther apart when connected by an optimal sequence of cow paths.
Suppose another field on the same plane is connected by cow paths as follows:
*F 30,15 / _/ _/ / *------ G H 25,10 30,10
In the scenario of just two fields on his farm, Farmer John would add a cow path between a point in each of these two fields (namely point sets {A,B,C,D,E} and {F,G,H}) so that the joined set of pastures {A,B,C,D,E,F,G,H} has the smallest possible diameter.
Note that cow paths do not connect just because they cross each other; they only connect at listed points.
The input contains the pastures, their locations, and a symmetric "adjacency" matrix that tells whether pastures are connected by cow paths. Pastures are not considered to be connected to themselves. Here's one annotated adjacency list for the pasture {A,B,C,D,E,F,G,H} as shown above:
A B C D E F G H A 0 1 0 0 0 0 0 0 B 1 0 1 1 1 0 0 0 C 0 1 0 0 1 0 0 0 D 0 1 0 0 1 0 0 0 E 0 1 1 1 0 0 0 0 F 0 0 0 0 0 0 1 0 G 0 0 0 0 0 1 0 1 H 0 0 0 0 0 0 1 0
Other equivalent adjacency lists might permute the rows and columns by using some order other than alphabetical to show the point connections. The input data contains no names for the points.
The input will contain at least two pastures that are not connected by any sequence of cow paths.
Find a way to connect exactly two pastures in the input with a cow path so that the new combined field has the smallest possible diameter of any possible pair of connected pastures. Output that smallest possible diameter.
PROGRAM NAME: cowtour
INPUT FORMAT
Line 1:An integer, N (1 <= N <= 150), the number of pasturesLine 2-N+1:Two integers, X and Y (0 <= X ,Y<= 100000), that denote that X,Y grid location of the pastures; all input pastures are unique.Line N+2-2*N+1:lines, each containing N digits (0 or 1) that represent the adjacency matrix as described above, where the rows' and columns' indices are in order of the points just listed.SAMPLE INPUT (file cowtour.in)
810 1015 1020 1015 1520 1530 1525 1030 100100000010111000010010000100100001110000000000100000010100000010
OUTPUT FORMAT
The output consists of a single line with the diameter of the newly joined pastures. Print the answer to exactly six decimal places. Do not perform any special rounding on your output.
SAMPLE OUTPUT (file cowtour.out)
22.071068
二 分析
三 代码
USER: Qi Shen [maxkibb3]TASK: cowtourLANG: C++Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 4396 KB] Test 2: TEST OK [0.000 secs, 4396 KB] Test 3: TEST OK [0.000 secs, 4396 KB] Test 4: TEST OK [0.000 secs, 4396 KB] Test 5: TEST OK [0.011 secs, 4396 KB] Test 6: TEST OK [0.011 secs, 4396 KB] Test 7: TEST OK [0.011 secs, 4396 KB] Test 8: TEST OK [0.011 secs, 4396 KB] Test 9: TEST OK [0.011 secs, 4396 KB]All tests OK.Your program ('cowtour') produced all correct answers! This is yoursubmission #3 for this problem. Congratulations!
/*ID:maxkibb3LANG:C++PROG:cowtour*/#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int MAX_PASTURE = 155;const double INF = 99999999;int n;int x[MAX_PASTURE], y[MAX_PASTURE];char mat[MAX_PASTURE][MAX_PASTURE];int f[MAX_PASTURE], f_num;double d[MAX_PASTURE][MAX_PASTURE];double dis[MAX_PASTURE];double base, ans = INF;void dfs(int idx) { if(f[idx] != 0) return; f[idx] = f_num; for(int i = 0; i < n; i++) { if(mat[idx][i] == '0') continue; dfs(i); }}double calc(int i, int j) { return sqrt((double)(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));}void floyd() { for(int i = 0; i < n; i++) { for(int j = i; j < n; j++) { if(i == j) d[i][j] = 0; else if (mat[i][j] == '0') d[i][j] = d[j][i] = INF; else d[i][j] = d[j][i] = calc(i, j); } } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { for(int k = 0; k < n; k++) { if(d[j][i] + d[i][k] < d[j][k]) d[j][k] = d[j][i] + d[i][k]; } } }}void init() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); } for(int i = 0; i < n; i++) { scanf("%s", mat[i]); }}int main() { freopen("cowtour.in", "r", stdin); freopen("cowtour.out", "w", stdout); init(); for(int i = 0; i < n; i++) { if(f[i] != 0) continue; f_num++; dfs(i); } floyd(); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(f[i] != f[j]) continue; if(d[i][j] > dis[i]) dis[i] = d[i][j]; } } for(int i = 0; i < n; i++) { if(dis[i] > base) base = dis[i]; } for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(f[i] == f[j]) continue; double candidate = calc(i, j) + dis[i] + dis[j]; candidate = max(candidate, base); if(candidate < ans) ans = candidate; } } printf("%.6f\n", ans); return 0;}
- usaco2.4.3 Cow Tours
- USACO2.4.3 Cow Tours (cowtour)
- USACO2.4.3 Cow Tours (cowtour)
- Cow Tours
- USACO Cow Tours 题解
- 【图论】【USACO】Cow Tours
- [U]Cow Tours
- usaco Cow Tours
- USACO:PROB Cow Tours
- USACO-Cow Tours
- USACO--2.4Cow Tours
- usaco Cow Tours
- usaco Training Cow Tours
- usaco2.32Cow Pedigrees
- USACO:Cow Tours解题报告
- [U]2.4.3 Cow Tours
- USACO 2.4.3 Cow Tours
- Usaco 2.4 Cow Tours(Floyd)
- jq框架封装学习笔记4-DOM操作模块
- Spark系列修炼---入门笔记25
- submit汉化 亲测可用
- Ubuntu系统安装matlab教程
- caffe之小小的CNN网络跑起来
- usaco2.4.3 Cow Tours
- python3 实现 linux 命令 tree
- Memcache源码阅读(4)---内存管理
- hgfs目录搭建及共享文件方法
- 台式机蓝牙适配器搜不到设备怎么办
- 什么是自组织特征映射网络?
- java 多线程学习笔记1-概念,实现方式
- DDMS中File Explorer目录不显示
- Java书籍Top 10