zoj 3080 ChiBi

本文介绍了一道关于火烧赤壁的算法题,该问题需要计算烧毁所有敌船所需的最短时间。通过Floyd算法预处理连通分量内的距离,并求得每个连通分量的半径来得到最终答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ChiBi

Time Limit: 5 Seconds      Memory Limit: 32768 KB

watashi's mm is so pretty as well as smart. Recently, she has watched the movie Chibi. So she knows more about the War of ChiBi. In the war, Cao Cao had 800,000 soldiers, much more than his opponents'. But he was defeated. One of the mistakes he made was that he connected some of his boats together, and these boats were burned by the clever opponents.

Then an interesting problem occurs to watashi's mm. She wants to use this problem to check whether watashi is as smart as her. However, watashi has no idea about the problem. So he turns to you for help.

You know whether two boats are directly connected and the distance between them. And Fire's speed to spread between boats is 1m/s. You also know the time your soldiers need to travel from your camp to each boat. Because burning Cao Cao's boat is a very dangerous job, you must choose the least number of soldiers, and each one can only burn one boat. How much time do you need to burn all the Cao Cao's boats?

Input

The input contains several test cases. Each test case begins with a line contains only one integer 0 <= N <= 1000, which indicates the number of boats. The next N lines, each line contains N integers in range [0, 10000], the jth number in the ith line is the distance in metre between the ith boat and the jth boat, if the number is -1, then these two boats are not directly connected (d(i, j) == d(j, i) && d(i, i) == 0). Then N intergers in range [0, 10000], the ith number is the time in second your soldiers need to travel from the camp to the ith boat. What's more Cao Cao is not that stupid, so he won't connect more than 100 boats together.

Output

The shortest time you need to burn all the Cao Cao's boats counting from the soldiers leave the camp in a single line.

Sample input

4
0 1 2 -1
1 0 4 -1
2 4 0 -1
-1 -1 -1 0
1 2 4 8

Sample Output

8

Author: CHAO, Jiansong

Source: ZOJ Monthly, December 2008


关键是求出每个连通分量的“半径”,由于题目保证每个连通分量大小不超过100,所以可以先用floyd求出连通分量内任意两点间的距离,再求答案即可。

#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<list>
#include<set>
#include<cmath>
using namespace std;
const int maxn = 1e3 + 5;
const int INF = 1e9;
const double eps = 1e-6;
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
#define fi first
#define se second

int dis[maxn][maxn];
vector<int> G[maxn];
int t[maxn];
int vis[maxn];
int n, cnt;

void dfs(int x){
    G[cnt].push_back(x);
    for(int i = 0;i < n;i++){
        if(!vis[i] && dis[x][i] != -1){
            vis[i] = 1;
            dfs(i);
        }
    }
}

int dp[maxn][maxn];
void floyd(int id){
    int Size = G[id].size();
    for(int i = 0;i < Size;i++){
        for(int j = 0;j < Size;j++){
            int x = G[id][i];
            int y = G[id][j];
            dp[i][j] = dis[x][y];
            if(dp[i][j] == -1)
                dp[i][j] = INF;
        }
    }
    for(int k = 0;k < Size;k++){
        for(int i = 0;i < Size;i++){
            for(int j = 0;j < Size;j++){
                dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
            }
        }
    }
}

int main(){
    while(cin >> n){
        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++)
                cin >> dis[i][j];
        }
        for(int i = 0;i < n;i++){
            cin >> t[i];
        }
        for(int i = 0;i < n;i++){
            G[i].clear();
        }
        memset(vis, 0, sizeof vis);
        cnt = 0;
        for(int i = 0;i < n;i++){
            if(!vis[i]){
                vis[i] = 1;
                dfs(i);
                cnt++;
            }
        }
        int ans = 0;
        for(int i = 0;i < cnt;i++){
            floyd(i);
            int Min = INF;
            for(int j = 0;j < G[i].size();j++){
                int x = G[i][j];
                int Max = 0;
                for(int k = 0;k < G[i].size();k++){
                    Max = max(Max, dp[j][k]);
                }
                Min = min(Min, t[x]+Max);
            }
            ans = max(ans, Min);
        }
        cout << ans << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值