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 的数组 A1,A2,A3,,Anm 个约束条件,约束条件有三种:

  • 1 x y cAxAyc
  • 2 x y cAxAyc
  • 3 x yAx=Ay

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

1n,m5×103

题解#

差分约束模板

可以尝试把 AxAyc 转化为 AyAxc,把 AxAyc 转化为 AxAy+c

Ax=Ay 可以转化为 AxAy=0,进而变成 AxAy0AxAy0

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

  1. xy,权值为 w
  2. yx,权值为 w
  3. xy,权值为 0
  4. yx,权值为 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 个部分和 l=1rAi,判断该数组是否合法(合法输出 "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 全源最短路#

题面#

题解#

代码#

作者:Daniel-yao

出处:https://www.cnblogs.com/Daniel-yao/p/17154440.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   Daniel_yzy  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示