Loading

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 的农场

题面

给定长度为 \(n\) 的数组 \(A_1,A_2,A_3,\dots,A_n\)\(m\) 个约束条件,约束条件有三种:

  • \(1\) \(x\) \(y\) \(c\)\(A_x - A_y \ge c\)
  • \(2\) \(x\) \(y\) \(c\)\(A_x - A_y \le c\)
  • \(3\) \(x\) \(y\)\(A_x = A_y\)

输出是否存在合法的自然数解。存在输出 "\(Yes\)", 不存在输出 "\(No\)"。

\(1 \le n,m \le 5 \times 10^3\)

题解

差分约束模板

可以尝试把 \(A_x - A_y \ge c\) 转化为 \(A_y \le A_x - c\),把 \(A_x - A_y \le c\) 转化为 \(A_x \le A_y + c\)

\(A_x = A_y\) 可以转化为 \(A_x - A_y = 0\),进而变成 \(A_x - A_y \le 0\)\(A_x - A_y \ge 0\)

每次遇到一个约束条件就将建 \(5\) 条边:

  1. \(x \to y\),权值为 \(-w\)
  2. \(y \to x\),权值为 \(w\)
  3. \(x \to y\),权值为 \(0\)
  4. \(y \to x\),权值为 \(0\)

建一个超级点,跑最短路即可。但不能用 \(dijkstra\),因为存在负环(或是要判负环)。

代码

#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]狡猾的商人

题面

对于一个长度为 \(n\) 的数组,给定 \(m\) 个部分和 \(\sum_{l=1}^{r} A_i\),判断该数组是否合法(合法输出 "\(Yes\)",不合法输出 "\(No\)")。

题解

代码

#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 全源最短路

题面

题解

代码

posted @ 2023-02-25 16:58  Daniel_yzy  阅读(29)  评论(0编辑  收藏  举报