USACO 2.4 Cow Tours (Floyd最短路径 + DFS)
来源:互联网 发布:2004年网络歌曲大全 编辑:程序博客网 时间:2024/05/29 13:42
#include <stdio.h>#include <math.h>#define DEBUG 1#define TESTCASES 9#define MAX_PASTUERS 150#define INF 1e10int numOfPastures;typedef struct Location{int x;int y;}Location;Location pastureArray[MAX_PASTUERS + 1];double distance[MAX_PASTUERS + 1][MAX_PASTUERS + 1];int field[MAX_PASTUERS + 1];//diameter[pasture]表示以牧区pasture为端点的直径,也就是牧区pasture到同个牧场的其他牧区的最长距离double diameter[MAX_PASTUERS + 1];double fieldDiameter[MAX_PASTUERS + 1];double getDistance(Location from, Location to){double deltaX = from.x - to.x;double deltaY = from.y - to.y;return sqrt(deltaX * deltaX + deltaY * deltaY);}void mark(int from, int fieldNum){if (field[from] != -1)return;field[from] = fieldNum;int to;for (to = 1; to <= numOfPastures; to++)if (distance[from][to] < INF)mark(to, fieldNum);}int main(){#if DEBUGint testCase;for (testCase = 1; testCase <= TESTCASES; testCase++){char inputFileName[20] = "inputX.txt";inputFileName[5] = '1' + (testCase - 1);freopen(inputFileName, "r", stdin);printf("\n#%d\n", testCase);#endifscanf("%d", &numOfPastures);int pasture;for (pasture = 1; pasture <= numOfPastures; pasture++)scanf("%d%d", &pastureArray[pasture].x, &pastureArray[pasture].y);//建图char cha;scanf("%c", &cha);int from, to;for (from = 1; from <= numOfPastures; from++){for (to = 1; to <= numOfPastures; to++){scanf("%c", &cha);if (from == to)distance[from][to] = 0.0;else if (cha == '0')distance[from][to] = INF;elsedistance[from][to] = getDistance(pastureArray[from], pastureArray[to]);}scanf("%c", &cha);}//Floyd求任意两点之间最短路径int pass;for (pass = 1; pass <= numOfPastures; pass++)for (from = 1; from <= numOfPastures; from++)for (to = from + 1; to <= numOfPastures; to++)if (distance[from][pass] + distance[pass][to] < distance[from][to])distance[from][to] = distance[to][from] = distance[from][pass] + distance[pass][to];for (pasture = 1; pasture <= numOfPastures; pasture++)field[pasture] = -1;int numOfFields = 0;//用DFS标识牧区pasture是属于哪个牧场fieldfor (pasture = 1; pasture <= numOfPastures; pasture++)if (field[pasture] == -1)mark(pasture, ++numOfFields);for (pasture = 1; pasture <= numOfPastures; pasture++)diameter[pasture] = 0;int fieldNum;for (fieldNum = 1; fieldNum <= numOfFields; fieldNum++)fieldDiameter[fieldNum] = 0;//求出diameter数组和牧场filed半径数组for (from = 1; from <= numOfPastures; from++){for (to = 1; to <= numOfPastures; to++)if (distance[from][to] < INF && distance[from][to] > diameter[from])diameter[from] = distance[from][to];if (diameter[from] > fieldDiameter[ field[from] ])fieldDiameter[ field[from] ] = diameter[from];}//连接牧场double minNewDiameter = INF;for (from = 1; from <= numOfPastures; from++)for (to = from + 1; to <= numOfPastures; to++){if (distance[from][to] != INF)continue;double newDiameter = diameter[from] + diameter[to] + getDistance(pastureArray[from], pastureArray[to]);//注意这样求出来的newDiameter有可能比连通后的新牧场半径小if (newDiameter < minNewDiameter)minNewDiameter = newDiameter;}//连通后的牧场半径不可能小于原牧场的半径for (fieldNum = 1; fieldNum <= numOfFields; fieldNum++)if (fieldDiameter[fieldNum] > minNewDiameter)minNewDiameter = fieldDiameter[fieldNum];printf("%.6lf\n", minNewDiameter);#if DEBUG}#endifreturn 0;}
0 0
- USACO 2.4 Cow Tours (Floyd最短路径 + DFS)
- Usaco 2.4 Cow Tours(Floyd)
- USACO-Section 2.4 Cow Tours(最短路[Floyd])
- USACO--2.4Cow Tours
- USACO Section 2.4 Cow Tours (Floyd+并查集)
- 【最短路径】洛谷 P1522 牛的旅行 Cow Tours
- USACO section 2.4 Cow Tours(并查集+最短路)
- USACO 2.4.3 Cow Tours
- USACO 2.4 Cow Tours (cowtour)
- USACO 2.4 Bessie Come Home (Floyd最短路径)
- USACO Cow Tours 题解
- 【图论】【USACO】Cow Tours
- usaco Cow Tours
- USACO:PROB Cow Tours
- USACO-Cow Tours
- usaco Cow Tours
- usaco Training Cow Tours
- Floyd 最短路径
- 在Xcode的模拟器中的textField中输入中文
- 条款14:在资源管理中心小心copy行为之重难点
- 证券基础--风险投资《阿凡达》的幕后英雄
- 黑马程序员——Java基础日记——面向对象
- complex-list代码解析
- USACO 2.4 Cow Tours (Floyd最短路径 + DFS)
- A016-布局之RelativeLayout
- 多线程NSThread状态控制—— 新建、就绪、运行、阻塞、死亡几个状态
- 80X86的物理地址形成(实模式+保护模式)——段式寻址
- 安卓控件使用系列34:ListView列表控件显示表单的使用方法1
- [python]学习笔记6-python数字
- 读《Effective Java》第一篇中文博客!
- 差旅费报销 总结
- c++桥接模式