跳到正文

个人记录

2025.7.21 图论小测四

#SUSPENDED#约 13 分钟
原文
文章信息与版本

文章信息

发布
2025年7月21日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 13 分钟

版本

  1. v1当前

概况

传送门

TATBTCTDTETF
100\color{green}{100}10\color{orange}{10}0\color{red}{0}0\color{red}{0}100\color{green}{100}0\color{red}{0}

考得较差,说明了一下问题:

  • 在未考虑单调性的情况下使用了二分算法

  • SPFA判环不熟练

  • 读题不仔细

分析

B

正确思路 本题为较重要的原题,因为发现 1≤c,f≤10001 ≤ c, f ≤ 1000 所以考虑通过枚举去掉(固定)一个边权。但由于要枚举 cc 的话没有枚举 ff 省时。于是枚举所有可能的 ff ,视其为一个限制,约束路径上的最小流量要 ≥≥ 所枚举的 ff。 对于每一个 ff ,跑 11 到 nn 的最短路,要求不经过流量小 ff 的边。最后取 1000000∗f/dis[n]1000000 * f / dis[n] 的最大值。

错误原因 在枚举 ff 的过程中使用二分,但枚举 ff 没有单调性。可见二分做法为假。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, m, dis[N];
struct node
{
	int v, c, f;
	bool operator < (const node & tmp) const{
		return c > tmp.c;
	}
};
vector<node> g[N];
priority_queue<node> q;
bool vis[N];
void init()
{
	for (int i = 1; i <= n; i++) dis[i] = 1e9;
	memset(vis, 0, sizeof(vis));
	while (!q.empty()) q.pop();
}
void Dijkstra(int p)
{
	init();
	dis[1] = 0;
	q.push({1, dis[1]});
	while (!q.empty())
	{
		int x = q.top().v;
		q.pop();
		if (vis[x]) continue;
		vis[x] = 1;
		for (int i = 0; i < g[x].size(); i++)
		{
			int y = g[x][i].v, z = g[x][i].c;
			int k = g[x][i].f;
			if (k >= p && dis[y] > dis[x] + z)
			{
				dis[y] = dis[x] + z;
				q.push({y, dis[y]}); 
			}
		} 
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	{
		int u, v, a, b;
		cin >> u >> v >> a >> b;
		g[u].push_back({v, a, b});
		g[v].push_back({u, a, b});
	}
	int mmax = -1;
	for (int i = 1; i <= 1000; i++)
	{
		Dijkstra(i);
		mmax = max(mmax, 1000000 * i / dis[n]);
	}
	cout << mmax; 
	return 0;
}

C

正确思路 先使用 map 将 string 类型的货币赋予一个编号。之后有向建边,检查其中是否存在有环切权值大于 11 。路径算边权乘积最大。每一个点都跑一编 SPFA 查环。

错误原因 使用了 Floyd

C++
#include <bits/stdc++.h>

using namespace std;
const int N = 35;
struct node
{
    int v;
    double w;
};
int n, m, cnt[N];
double dis[N];
bool vis[N];
vector<node> g[N];
queue<int> q;
map<string, int> id;
void init()
{
    for (int i = 1; i <= n; i++) g[i].clear();
    for (int i = 1; i <= n; i++) dis[i] = -1e9;
    while (!q.empty()) q.pop();
    memset(vis, 0, sizeof(vis));
    return ;
}
bool SPFA(int s)
{
    dis[s] = 1.0;
    cnt[s] = 0;
    q.push(s);
    vis[s] = 1;
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for (int i = 0; i < g[x].size(); i++)
        {
            int y = g[x][i].v;
            double z = g[x][i].w * 1.0;
            if (dis[x] * z * 1.0 > dis[y])
            {
                dis[y] = dis[x] * z * 1.0;
                if (!vis[y])
                {
                    q.push(y);
                    vis[y] = 1;
                }
                cnt[y] = cnt[x] + 1;
                if (cnt[y] > n - 1) return 1;
            }
        }
    }
    return 0;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int cn = 1;
    while (1)
    {
        cin >> n;
        if (n == 0) exit(0);
        init();
        cout << "Case " << cn << ": ";
        cn++;
        for (int i = 1; i <= n; i++)
        {
            string s;
            cin >> s;
            id[s] = i;
        }
        cin >> m;
        for (int i = 1; i <= m; i++)
        {
            string a, b;
            double x;
            cin >> a >> x >> b;
            int u = id[a], v = id[b];
            g[u].push_back({v, x});
        }
        bool flag = 1;
        for (int i = 1; i <= n; i++)
        {
            bool as = SPFA(i);
            if (as)
            {
                cout << "Yes\n";
                flag = 0;
                break;
            } 
        }
        if (flag) cout << "No\n";
    }
    return 0;
}

D

正确思路 先二维转一维。再枚举 dd ,使用并查集判断在限制 dd 内的所有路标是否联通即可

错误原因 时间不足

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 5e2 + 10;
const int NM = 5e2 * 5e2 + 10;
int n, m, fa[NM], a[N][N], start[NM], cnt = 0;
int find(int x)
{
    if (fa[x] == x) return x;
    return fa[x] = find(fa[x]);
}
void unionn(int x, int y)
{
    x = find(x); y = find(y);
    fa[x] = y;
}
int get_id(int x, int y)
{
    return (x - 1) * m + y;
}
void add(int i, int j, int x, int y)
{
    int a = get_id(i, j);
    int b = get_id(x, y);
    unionn(a, b);
}
bool check(int d)
{
    for (int i = 1; i <= n * m; i++) fa[i]= i;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            if (i < n && abs(a[i][j] - a[i + 1][j]) <= d) add(i, j, i + 1, j);
            if (j < m && abs(a[i][j] - a[i][j + 1]) <= d) add(i, j, i, j + 1);
        }
    for (int i = 2; i <= cnt; i++)
        if (find(start[i]) != find(start[i - 1])) return 0;
    return 1;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    int l = -1, r = 0, mid;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
            r = max(r, a[i][j]);
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        {
            int x;
            cin >> x;
            if (x == 1) start[++cnt] = get_id(i, j);
        }
    r++;
    while (l + 1 < r)
    {
        mid = (l + r) >> 1;
        if (check(mid)) r = mid;
        else l = mid;
    }
    cout << r;
    return 0;
}

F

正确思路 基本上是 ST表 的板子

错误原因 忽略了大小写问题

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 5e4 + 10;
int n, m, a[N], dp[N][30], lg[N];
map<string, int> si;
map<int, string> is;
string s[N], s1[N];
void ST()
{
	for (int i = 1; i <= n; i++) dp[i][0] = si[s[i]];
	for (int j = 1; (1 << j) <= n; j++)
		for (int i = 1; i + (1 << j) - 1 <= n; i++)
			dp[i][j] = max(dp[i][j - 1], dp[i + (1 << j - 1)][j - 1]);
	return ; 
}
bool cmp(string a, string b)
{
	for (int i = 0; i < a.size(); i++)
		if (a[i] >= 'A' && a[i] <= 'Z') a[i] += 32;
	for (int i = 0; i < b.size(); i++)
		if (b[i] >= 'A' && b[i] <= 'Z') b[i] += 32;
	return a < b;
}
int main()
{
//	freopen("F.in", "r", stdin);
//	freopen("F.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n >> m;
	lg[0] = -1;
	for (int i = 1; i <= n; i++)
	{
		cin >> s[i];
		s1[i] = s[i];
		lg[i] = lg[i / 2] + 1;
	}
	sort(s1 + 1, s1 + n + 1, cmp);
	for (int i = 1; i <= n; i++)
	{
		si[s1[i]] = i;
		is[i] = s1[i];
	}
	ST();
	while (m--)
	{
		int x, y;
		cin >> x >> y;
		int len = y - x + 1;
		int j = lg[len];
		cout << is[max(dp[x][j], dp[y - (1 << j) + 1][j])] << "\n"; 
	}
	return 0;
}
原文