USACO 2.4 Cow Tours (cowtour)
2014-02-25 13:56
615 查看
//Main Idea //Use floyd algorithm to calculate shortest path of each pair of pastures //The compute the smallest possible diameter of the new connected field by enumeration //The key point is that there are exactly two field need to be connected; /* ID: haolink1 PROG: cowtour LANG: C++ */ //#include <iostream> #include <fstream> #include <math.h> /* sqrt */ #include<iomanip> //std::setiosflags #define INF (1e40) using namespace std; short num =0; //dis[i][j] means the shortest distance between i,j; //dis_max[i] means the max distance of shortest path start from i; double dis[150][150],dis_max[150]; int px[150],py[150]; double dist(int x1,int y1,int x2,int y2){ return sqrt(double(x1-x2)*(x1-x2) + double(y1-y2)*(y1-y2)); } void floyd(){ for(short k = 0; k < num; k++){ for(short i = 0; i < num; i++){ for(short j =0; j < num; j++){ if(dis[i][k]+dis[k][j] < dis[i][j]) dis[i][j] = dis[i][k]+dis[k][j]; } } } //Note: reset the dis[i][i] to INF in case //it will influence the dis_max[i]; for(short i = 0; i < num; i++){ dis[i][i] = INF; } } /* void PrintDist(){ for(short i = 0; i < num; i++){ for(short j = 0; j < num; j++){ cout<< dis[i][j] <<" "; } cout << endl; } } */ int main(){ ifstream fin("cowtour.in"); fin >> num; for(short i = 0; i < num; i++){ fin >> px[i]; fin >> py[i]; } for(short i = 0; i < num; i++){ char val[150]; fin >> val; for(short j = 0; j < num; j++){ if(val[j] == '1'){ dis[i][j] = dist(px[i],py[i],px[j],py[j]); }else{ dis[i][j] = INF; } } } //PrintDist(); floyd(); //PrintDist(); double d_max = 0; for(short i = 0; i < num; i++){ double tmp_max = 0; for(short j = 0; j < num; j++){ if(dis[i][j] > tmp_max && dis[i][j] < INF) tmp_max = dis[i][j]; } dis_max[i] = tmp_max; if(tmp_max > d_max) d_max = tmp_max; } double d_min = INF; for(short i = 0; i < num-1; i++){ for(short j = i+1; j < num; j++){ if(i != j && dis[i][j] == INF){ double temp = dist(px[i],py[i],px[j],py[j]); if(temp+dis_max[i]+dis_max[j] < d_min) d_min = temp+dis_max[i]+dis_max[j]; } } } ofstream fout("cowtour.out"); //fout <<setiosflags(ios::fixed)<<setiosflags(ios::right)<<setprecision(6)<<(d_max > d_min ? d_max:d_min) << endl; fout <<setiosflags(ios::fixed)<<setprecision(6)<<(d_max > d_min ? d_max:d_min) << endl; return 0; }
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- [转]可视化的数据结构和算法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- 使用他人的MD5编码类,修改形成密码串
- Extracting Structured Data from Web Pages
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘
- Java中3DES加密
- Refactoring Notes-Refactoring Methods(3)
- 图书馆管理程序~~不过貌似功能!!有空再修修
- trainging contest#2(2011成都现场赛)I BY Hyoga
- C/C++头文件包含内容概览
- 堆栈的应用(1) 平衡符号 C++实现
- 程序员编程艺术第一章、左旋转字符串
- 程序员编程艺术:第三章续、Top K算法问题的实现
- 程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数
- 十四、第三章再续:快速选择SELECT算法的深入分析与实现
- 程序员编程艺术:第七章、求连续子数组的最大和
- 程序员编程艺术:第八章、从头至尾漫谈虚函数
- 程序员编程艺术:第九章、闲话链表追赶问题
- 程序员编程艺术:第十章、如何给10^7个数据量的磁盘文件排序