USACO 2.4 Cow Tours (cowtour)

//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;
}

回答: 题目P1518 \[USACO2.4\] 两只塔姆沃斯牛是一道模拟题,题目要求判断Farmer John和两头牛是否会相遇。解题思路可以分为两种方法。一种方法是记录二者的状态,如果出现了与前面相同的状态则说明陷入了死循环。具体实现步骤可以使用数组来记录Farmer John和两头牛的坐标、方向等状态信息,然后判断是否出现了重复的状态。另一种方法是利用博弈的思想,如果二者会同时回到一种状态,那么说明他们不会再相遇了,因为这时候他们已经陷入了一种对称性的状态。通过判断是否存在一种线性关系,可以确定二者是否会相遇。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two](https://blog.csdn.net/TD123456q/article/details/125688037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two 题解 (C/C++)](https://blog.csdn.net/Jason__Jie/article/details/115027619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [(移动方向状态标志)P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two题解](https://blog.csdn.net/m0_57221330/article/details/119980758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值