2020牛客多校第二场I题
首先,我们考虑最小割的方式来处理该问题:
很明显的,这就是一张对偶图了,因为它没有任意两线会存在相交的可能了。
所以根据对偶图的做法,我们可以将最小割问题转化为最短路了:
绿色和粉色是新的对偶图所构成的边和点。
然后我们在对偶图上跑一个最短路就可以了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 25e4 + 7, maxM = 4 * maxN;
int N, M, head[maxN], cnt, n, S, T, id[505][505];
struct Eddge
{int nex, to; ll val;Eddge(int a=-1, int b=0, ll c=0):nex(a), to(b), val(c) {}
} edge[maxM << 1];
inline void addEddge(int u, int v, ll w)
{edge[cnt] = Eddge(head[u], v, w);head[u] = cnt++;
}
inline void _add(int u, int v, ll w) { addEddge(u, v, w); addEddge(v, u, w); }
int cost_mp[505][505][2] = {0};
struct node
{int id; ll val;node(int a=0, ll b=0):id(a), val(b) {}friend bool operator < (node e1, node e2) { return e1.val > e2.val; }
} now;
priority_queue<node> Q;
ll dis[maxN];
inline ll Dijkstra()
{for(int i=1; i<=n; i++) dis[i] = INF;dis[S] = 0;Q.push(node(S, 0));while(!Q.empty()){now = Q.top(); Q.pop();int u = now.id;if(dis[u] < now.val) continue;for(int i=head[u], v; ~i; i=edge[i].nex){v = edge[i].to;if(dis[v] > dis[u] + edge[i].val){dis[v] = dis[u] + edge[i].val;Q.push(node(v, dis[v]));}}}return dis[T];
}
int main()
{scanf("%d%d", &N, &M);n = 0;for(int i=1; i<N; i++){for(int j=1; j<=i; j++){id[i][j] = ++n;}}S = ++n; T = ++n;cnt = 0;for(int i=1; i<=n; i++) head[i] = -1;int l, r, x, y; char op[3];for(int i=1; i<=M; i++){scanf("%d%d%s", &l, &r, op);x = N - (r - l);y = r + x - N;scanf("%d", &cost_mp[x][y][op[0] == 'L']);}for(int i=1; i<N; i++){if(cost_mp[i][1][0]) _add(S, id[i][1], cost_mp[i][1][0]);if(i > 1){if(cost_mp[i][1][1]) _add(id[i][1], id[i - 1][1], cost_mp[i][1][1]);}for(int j=2; j < i; j++){if(cost_mp[i][j][0]) _add(id[i - 1][j - 1], id[i][j], cost_mp[i][j][0]);if(cost_mp[i][j][1]) _add(id[i][j], id[i - 1][j], cost_mp[i][j][1]);}if(i > 1){if(cost_mp[i][i][0]) _add(id[i - 1][i - 1], id[i][i], cost_mp[i][i][0]);}if(cost_mp[i][i][1]) _add(id[i][i], T, cost_mp[i][i][1]);}ll ans = Dijkstra();if(ans >= INF) ans = -1;printf("%lld\n", ans);return 0;
}