2023 ICPC 网络赛 第二场 部分题解 (待完善)

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值