usaco2.4.3 Cow Tours

来源:互联网 发布:淘宝买家诈骗怎么办 编辑:程序博客网 时间:2024/06/06 19:29

一 原题

Cow Tours

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


二 分析

dfs找联通块+floyd最短路


三 代码

运行结果:
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!


AC代码:
/*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;}



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 支付宝充错手机账号怎么办 美团恶意差评怎么办 买家好评后追加差评怎么办 宝贝吃了一个金币怎么办 店铺微淘等级l1怎么办 淘宝占内存2个g怎么办 淘宝太占空间了怎么办 支付宝占内存大怎么办 苹果手机储存空间不足怎么办 小米平板电脑储存空间不足怎么办 ipad2很卡反应慢怎么办 ipadmini很卡反应慢怎么办 手机酷狗音乐文件不支持怎么办 2018款ipad闪退怎么办 ipad开不了机了怎么办 淘宝盖楼上限了怎么办 交了学费做微淘客却加不到人怎么办 微淘客交首付不想做了怎么办 蚂蚁微客二维码推广怎么办 游拍主播申请手机号被注册怎么办 淘宝客不给力怎么办 淘宝买家确认收货超时怎么办 淘宝没收到货退款卖家不处理怎么办 微博红包都是字怎么办 500个访客没转化怎么办 店铺动态评分是0怎么办 京东店铺评分低怎么办 被淘宝主播屏蔽怎么办 在淘宝客推广后退款怎么办 生产出现异常时你应该怎么办 违规后的店铺没访客怎么办 淘宝少发货店家不承认怎么办 淘宝买东西店家不发货怎么办 淘宝店家拒绝同意退款怎么办 被淘宝店家骂了怎么办 淘宝买家骂店家骚扰店家怎么办? 不想开淘宝店了怎么办 我是客服经常有客户骂人怎么办 淘宝直播前期没人看怎么办 淘宝被投诉商标侵权怎么办 淘宝后商家页面打不开了怎么办