neuq-acm预备队训练week 8 P8794 [蓝桥杯 2022 国 A] 环境治理

2023-12-14 21:56:14

题目描述

输入格式

输出格式

输出一行包含一个整数表示答案。

输入输出样例

解题思路

最短路+二分

AC代码

#include<bits/stdc++.h>
using namespace std;
long long temp,n, Q;
long long f[105][105],min_f[105][105],cut[105],dis[105][105];//cut为减少多少,dis表示减少后的
bool check(long long day)
{
    temp = 0;//赋初值
    long long times = day / n;
    long long now = day%n;
    for (int i = 1; i <= n; i++)
    {
        if (i <= now)cut[i] = times + 1;
        else cut[i] = times;
    }
    
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            dis[i][j] = max(f[i][j] - cut[i] - cut[j], min_f[i][j]);
        }
    }
    for (int k = 1; k <= n; k++)//Floyd模板
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            temp += dis[i][j];
        }
    }
    if (temp <= Q)return true;
    else return false;
}
int main()
{
    cin >> n>>Q;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> f[i][j];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> min_f[i][j];
        }
    }
    long long left = 0, right = 1e9,mid;
    while (left<right)
    {
        mid = (left + right) / 2;
        if (check(mid))  right = mid;
        else left = mid + 1;
    }
    if (left == 1e9)cout << "-1" << endl;
    else cout << left << endl;
    return 0;
}

文章来源:https://blog.csdn.net/ciwen_/article/details/134916957
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。