这道题的建模很有意思:
1.首先每个点,除了起点,都向拆点后的点练一天容量1,费用-1的边,代表只能走一次,负费用是为了求最大的费用,也就是最多的经过点。对于起点,容量变成2,费用不变。
2.其次对于一天航线,终点在拆点后的点集向起点连一条边,容量无限,费用为0,代表两个城市可以达到。
之后一次费用流,如果可以到达,说明最大流肯定是2,将起点的次数排除,答案就是2 - 最小费用(因为费用是负的)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost,from;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
N = n;
tol =