D Project Manhattan
思路:
最终选中的下标,必然包含n个行或者n个列.
所以答案 = n行的最小值之和或者n列的最小值之和
注意坑点: 当存在负数时,应该把负数全部选上,答案只会更优.
代码:
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define int long long
#define endl '\n'
#define bit(x) (1ll << x)
using namespace std;
const int N = 1e6 + 5;
const int inf = 1e16;
void solve()
{
int n;
cin >> n;
vector<vector<int>> a(n + 1, vector<int>(n + 1));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
int ans1 = 0;
int ans2 = 0;
for (int i = 1; i <= n; i++)
{
vector<int> c(n + 1);
for (int j = 1; j <= n; j++)
{
c[j] = a[i][j];
}
sort(c.begin() + 1, c.end());
ans1 += c[1];
int now = 2;
while (c[now] < 0)
{
ans1 += c[now];
now++;
}
}
for (int i = 1; i <= n; i++)
{
vector<int> c(n + 1);
for (int j = 1; j <= n; j++)
{
c[j] = a[j][i];
}
sort(c.begin() + 1, c.end());
ans2 += c[1];
int now = 2;
while (c[now] < 0)
{
ans2 += c[now];
now++;
}
}
cout << min(ans1,ans2) << endl;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}
E Another Bus Route Problem
题解:
下面这些话比较抽象, 具体看代码.
记忆化BFS.
对于一条路径(u,v), 不断的找他们的LCA,并标记中途经过的点,如果途中经过的点是线路的话,就加入队列中,然后把步数+1即可.
会有两种情况:
1.整条路径(u,v)都没有被访问过:直接标记即可
2.找LCA的过程中,已经有顶点被标记过了.那么说明他们的LCA也必然被标记过了,否则不可能出现这种状态.没被标记的那个条路径继续往上找.
#include <bits/stdc++.h>