1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
   | #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define sqz main #define ll long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define Rep(i, a, b) for (int i = (a); i < (b); i++) #define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)
  const ll INF = 1e9, Mo = 998244353; const int N = 30005; const double eps = 1e-6; namespace slow_IO {     ll read()     {         ll x = 0; int zf = 1; char ch = getchar();         while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();         if (ch == '-') zf = -1, ch = getchar();         while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();         return x * zf;     }     void write(ll y)     {         if (y < 0) putchar('-'), y = -y;         if (y > 9) write(y / 10);         putchar(y % 10 + '0');     } } using namespace slow_IO;
  int head[N + 5], Dis[N + 5], Flag[N + 5], Q[N + 5], Vis[N + 5]; int edgenum = 0, n, m, k; ll ans; struct node {     int vet, next, val; }edge[3 * N + 5]; void addedge(int u, int v, int w) {     edge[edgenum].vet = v;     edge[edgenum].val = w;     edge[edgenum].next = head[u];     head[u] = edgenum++; }
  int spfa() {     int Head = 0, Tail = 1;     Q[0] = 0, Flag[0] = 1, Vis[0] = 1, Dis[0] = 0;     while (Head != Tail)     {         int u = Q[Head]; Head = (Head + 1) % N;         travel(i, u)         {             int v = edge[i].vet;             if (Dis[u] + edge[i].val > Dis[v])             {                 Dis[v] = Dis[u] + edge[i].val;                 if (++Vis[v] >= n) return 0;                 if (!Flag[v])                 {                     Q[Tail] = v, Flag[v] = 1;                     Tail = (Tail + 1) % N;                 }             }         }         Flag[u] = 0;     }     return 1; }
  int sqz() {     n = read(), m = read();     rep(i, 0, n) head[i] = -1, Dis[i] = -INF, Flag[i] = 0;     rep(i, 1, m)     {         int op = read();         if (op == 1)         {             int a = read(), b = read(), c = read();             if (a == b)             {                 puts("No");                 return 0;             }             addedge(b, a, c);         }         if (op == 2)         {             int a = read(), b = read(), c = read();             if (a == b)             {                 puts("No");                 return 0;             }             addedge(a, b, -c);         }         if (op == 3)         {             int a = read(), b = read();             addedge(a, b, 0), addedge(b, a, 0);         }     }     rep(i, 1, n) addedge(0, i, 0);     if (!spfa()) puts("No");     else puts("Yes");     return 0; }
   |