#include <bits/stdc++.h>
using namespace std;
#define orz puts("---------------------")
template <typename Ty, int Siz>
struct Graph {
struct Edge {
int to, nxt;
Ty w, cost;
} e[Siz << 1];
int head[Siz], now[Siz], cnt;
int S, T;
Ty inf;
Ty dis[Siz];
int vis[Siz], tag[Siz];
void init() {
memset(head, 0, sizeof(head));
cnt = 0;
}
void change(int _S, int _T, Ty _inf) {
S = _S, T = _T, inf = _inf;
}
int gt(int x) {return x + (x & 1 ? 1 : -1);}
void add(int u, int v, Ty w, Ty c) {
e[++cnt] = {v, head[u], w, c};
head[u] = cnt;
}
void add_edge(int u, int v, Ty w, Ty c) {
add(u, v, w, c);
add(v, u, 0, -c);
}
void add_Edge(int u, int v, Ty w, Ty c) {
add_edge(u, v, w, c);
add_edge(v, u, w, c);
}
bool bfs() {
memset(dis, 0x3f, sizeof(dis));
Ty qwq = dis[0];
memset(vis, 0, sizeof(vis));
memset(tag, 0, sizeof(tag));
dis[S] = 0;
now[S] = head[S];
vis[S] = 1;
queue <int> q;
q.push(S);
//orz;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
//cout << x << endl;
for (int i = head[x]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[x] + e[i].cost && e[i].w > 0) {
dis[v] = dis[x] + e[i].cost;
now[v] = head[v];
if (!vis[v]) vis[v] = 1, q.push(v);
}
}
}
return dis[T] != qwq;
}
Ty ans;
Ty dfs(int x, Ty flow) {
if (x == T) {
ans += flow * dis[T];
return flow;
}
tag[x] = 1;
Ty sum = 0;
for (int i = now[x]; i; i = e[i].nxt) {
int v = e[i].to;
now[x] = i;
if (!tag[v] && dis[v] == dis[x] + e[i].cost && e[i].w > 0) {
Ty tmp = dfs(v, min(e[i].w, flow - sum));
sum += tmp;
e[i].w -= tmp;
e[gt(i)].w += tmp;
if (sum == flow) break;
}
}
if (sum == flow) tag[x] = 0;
return sum;
}
Ty dinic() {
ans = 0;
Ty sum = 0;
while (bfs()) sum += dfs(S, inf);
cout << ans << endl;
return sum;
}
};
Graph <long long, 100005> G;
int n, m, s, t;
signed main() {
// cin >> n >> m >> s;
// G.change(0, n + 1, 1e9);
// for (int i = 1, u; i <= n; i++) {
// cin >> u;
// G.add_edge(i, G.T, u, 0);
// }
// for (int i = 1, d; i <= n; i++) {
// cin >> d;
// G.add_edge(G.S, i, G.inf, d);
// }
// for (int i = 1; i < n; i++) G.add_edge(i, i + 1, G.S, m);
// printf("%d", G.dinic());
cin >> n >> m >> s >> t;
G.change(s, t, 1e18);
for (int i = 1, u, v, w, c; i <= m; i++) {
cin >> u >> v >> w >> c;
G.add_edge(u, v, w, c);
}
cout << G.dinic() << ' ' << G.ans;
return 0;
}
如图,此时本人的代码跑板子会输出 50 0
题目是这个 qwq
而如果换成如下:
#include <bits/stdc++.h>
using namespace std;
#define orz puts("---------------------")
template <typename Ty, int Siz>
struct Graph {
struct Edge {
int to, nxt;
Ty w, cost;
} e[Siz << 1];
int head[Siz], now[Siz], cnt;
int S, T;
Ty inf;
Ty dis[Siz];
int vis[Siz], tag[Siz];
void init() {
memset(head, 0, sizeof(head));
cnt = 0;
}
void change(int _S, int _T, Ty _inf) {
S = _S, T = _T, inf = _inf;
}
int gt(int x) {return x + (x & 1 ? 1 : -1);}
void add(int u, int v, Ty w, Ty c) {
e[++cnt] = {v, head[u], w, c};
head[u] = cnt;
}
void add_edge(int u, int v, Ty w, Ty c) {
add(u, v, w, c);
add(v, u, 0, -c);
}
void add_Edge(int u, int v, Ty w, Ty c) {
add_edge(u, v, w, c);
add_edge(v, u, w, c);
}
bool bfs() {
memset(dis, 0x3f, sizeof(dis));
Ty qwq = dis[0];
memset(vis, 0, sizeof(vis));
memset(tag, 0, sizeof(tag));
dis[S] = 0;
now[S] = head[S];
vis[S] = 1;
queue <int> q;
q.push(S);
//orz;
while (!q.empty()) {
int x = q.front();
q.pop();
vis[x] = 0;
//cout << x << endl;
for (int i = head[x]; i; i = e[i].nxt) {
int v = e[i].to;
if (dis[v] > dis[x] + e[i].cost && e[i].w > 0) {
dis[v] = dis[x] + e[i].cost;
now[v] = head[v];
if (!vis[v]) vis[v] = 1, q.push(v);
}
}
}
return dis[T] != qwq;
}
Ty ans = 0;
Ty dfs(int x, Ty flow) {
if (x == T) {
ans += flow * dis[T];
return flow;
}
tag[x] = 1;
Ty sum = 0;
for (int i = now[x]; i; i = e[i].nxt) {
int v = e[i].to;
now[x] = i;
if (!tag[v] && dis[v] == dis[x] + e[i].cost && e[i].w > 0) {
Ty tmp = dfs(v, min(e[i].w, flow - sum));
sum += tmp;
e[i].w -= tmp;
e[gt(i)].w += tmp;
if (sum == flow) break;
}
}
if (sum == flow) tag[x] = 0;
return sum;
}
pair<Ty, Ty> dinic() {
ans = 0;
Ty sum = 0;
while (bfs()) sum += dfs(S, inf);
return {sum, ans};
}
};
Graph <long long, 100005> G;
int n, m, s, t;
signed main() {
// cin >> n >> m >> s;
// G.change(0, n + 1, 1e9);
// for (int i = 1, u; i <= n; i++) {
// cin >> u;
// G.add_edge(i, G.T, u, 0);
// }
// for (int i = 1, d; i <= n; i++) {
// cin >> d;
// G.add_edge(G.S, i, G.inf, d);
// }
// for (int i = 1; i < n; i++) G.add_edge(i, i + 1, G.S, m);
// printf("%d", G.dinic());
cin >> n >> m >> s >> t;
G.change(s, t, 1e18);
for (int i = 1, u, v, w, c; i <= m; i++) {
cin >> u >> v >> w >> c;
G.add_edge(u, v, w, c);
}
pair<long long, long long> qwq = G.dinic();
cout << qwq.first << ' ' << qwq.second;
return 0;
}
输出的就是正确的答案 50 280
但是我觉得这两种写法有不同吗?第一种为什么会寄?