test20230225考试总结(2023春 · 图论)
前言#
I hate questions that already exist!!
我讨厌原题!!
赛时得分明细:
A | B | C | D | E | F | Total | Rank |
---|---|---|---|---|---|---|---|
100 | 100 | 10 | 56 | 0 | 44 | 310 | 6 |
A. P1993 小 K 的农场#
题面#
给定长度为
: ; : ; : ;
输出是否存在合法的自然数解。存在输出 "
题解#
差分约束模板
可以尝试把
每次遇到一个约束条件就将建
,权值为 ; ,权值为 ; ,权值为 ; ,权值为 ;
建一个超级点,跑最短路即可。但不能用
代码#
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
inline int read() {
rint x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 1e6 + 10;
struct Node {
int v, w;
};
int n, m, q[N], dist[N], cnt[N];
bool st[N];
vector <Node> e[N];
bool spfa() {
int h = 1, t = 0;
memset(dist, 0x3f, sizeof dist);
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
q[++t] = 0, st[0] = 1, dist[0] = 0;
while(h <= t) {
int x = q[h++];
st[x] = 0;
for (int i = 0; i < e[x].size(); i++) {
int y = e[x][i].v, w = e[x][i].w;
if(dist[y] > dist[x] + w) {
dist[y] = dist[x] + w;
cnt[y] = cnt[x] + 1;
if(cnt[y] >= n + 1) return 0;
if(!st[y]) {
q[++t] = y;
st[y] = 1;
}
}
}
}
return 1;
}
void add(int u, int v, int w) {
e[u].push_back((Node){v, w});
}
signed main() {
n = read(), m = read();
For(i,1,m) {
int op = read(), x = read(), y = read();
if(op == 1) {
int w = read();
add(x, y, -w);
} else if(op == 2) {
int w = read();
add(y, x, w);
} else {
add(x, y, 0);
add(y, x, 0);
}
}
For(i,1,n) add(0, i, 0);
puts(spfa()?"Yes":"No");
return 0;
}
B. P2294 [HNOI2005]狡猾的商人#
题面#
对于一个长度为
题解#
代码#
#include <bits/stdc++.h>
#define int long long
#define H 19260817
#define rint register int
#define For(i,l,r) for(rint i=l;i<=r;++i)
#define FOR(i,r,l) for(rint i=r;i>=l;--i)
#define MOD 1000003
#define mod 1000000007
using namespace std;
inline int read() {
rint x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
void print(int x){
if(x<0){putchar('-');x=-x;}
if(x>9){print(x/10);putchar(x%10+'0');}
else putchar(x+'0');
return;
}
const int N = 2e5 + 10;
int n, m, Q, f[N], val[N];
int find(int x) {
if(x == f[x]) return x;
else {
int root = find(f[x]);
val[x] += val[f[x]];
return f[x] = root;
}
}
signed main() {
Q = read();
while(Q--) {
bool vis = 0;
n = read(), m = read();
For(i,0,n) f[i] = i, val[i] = 0;
For(i,1,m) {
int l = read(), r = read(), s = read();
l--;
int t1 = find(l), t2 = find(r);
if(t1 != t2) {
f[t2] = t1;
val[t2] = val[l] + s - val[r];
} else {
if(val[r] - val[l] != s) {
vis = 1;
puts("false");
break;
}
}
}
if(!vis) puts("true");
}
return 0;
}
C. P7624 [AHOI2021初中组] 地铁#
题面#
题解#
代码#
D. P6378 [PA2010] Riddle#
题面#
题解#
代码#
E. P3513 [POI2011] KON-Conspiracy#
题面#
题解#
代码#
F. P5905 【模板】Johnson 全源最短路#
题面#
题解#
代码#
作者:Daniel-yao
出处:https://www.cnblogs.com/Daniel-yao/p/17154440.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】