题目描述
输入格式
输出格式
输出一行包含一个整数表示答案。
输入输出样例
解题思路
最短路+二分
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;
}