POJ2135 Farm Tour

嘟嘟嘟


费用流入门题。
其实我也不知道为啥是费用流,不过因为学费用流的时候推这题了我才能想到。


因为每一条路只能走一次,所以容量设为1,路径长度作为费用。
然后从源点向1号节点连一条容量为2,费用为0的边;从\(n\)号节点向汇点连一条容量为2,费用为0的边。
跑最小费用流即可。
```c++

include

include

include

include

include

include

include

include

include

include

using namespace std;

define enter puts("")

define space putchar(' ')

define Mem(a, x) memset(a, x, sizeof(a))

define rg register

typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e3 + 5;
const int maxm = 1e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}

int n, m, s, t;
struct Edge
{
int nxt, from, to, cap, c;
}e[maxm << 2];
int head[maxn], ecnt = -1;
void addEdge(int x, int y, int w, int f)
{
e[++ecnt] = (Edge){head[x], x, y, w, f};
head[x] = ecnt;
e[++ecnt] = (Edge){head[y], y, x, 0, -f};
head[y] = ecnt;
}

queue

void MCMF(int s, int t)
{
while(spfa(s, t)) update(s, t);
}

int main()
{
Mem(head, -1);
n = read(); m = read(); s = 0; t = n + 1;
for(int i = 1; i <= m; ++i)
{
int x = read(), y = read(), f = read();
addEdge(x, y, 1, f); addEdge(y, x, 1, f);
}
addEdge(s, 1, 2, 0); addEdge(n, t, 2, 0);
MCMF(s, t);
write(minCost), enter;
return 0;
}```

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10009612.html