2020 Niuke Summer Multi-School Training Camp Second Session I Interval Problem Solution

2020 Niu Ke Summer Multi-School Training Camp Second Session I Interval

Question : There are two operations for the interval [l, r]. The first is to reduce the interval and turn it into [l+1,r] or [l,r+1]. The second is to expand the interval. It becomes [l-1,r] or [l,r+1]. There are two blocking operations (l,r,dir,c). The first type of dir='L' can make [l,r ] Does not become [l+1,r], the second type of dir='R' ​​can make [l,r] not become [l,r-1], and the cost is both c. The starting point is [1,n]. To make it impossible to reach the form of [i,i] during the operation, ask what is the minimum cost.
Solution : First of all, this is a grid graph. The starting point is the upper right corner and the ending point is a point on the diagonal. To prevent the starting point from reaching the end point, some blocking operations are equivalent to cutting off some edges. How to build an edge, see the picture.

3 4
1 3 L 10
1 3 R 3
1 2 L 1
1 2 R 1

Insert picture description here
In fact, it is to establish a super source and a super sink, and then the meaning of each side is to cut off this side, and the shortest path from s to t on this side. The thought used is the idea of ​​minimum cutting. The wolf catching rabbit is very similar to this question, but the person who wrote the question seems to be stuck with the minimum cutting method. My ISAP has been segfaulting .

Code :

#include<bits/stdc++.h>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-8
#define rint register int
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {
    
    
  int f = 1; res = 0;
  char c = getchar();
  while(c < '0' || c > '9') {
    
     if(c == '-') f = -1; c = getchar(); }
  while(c >= '0' && c <= '9') {
    
     res = res * 10 + c - '0'; c = getchar(); }
  res *= f;
}
const int ne[8][2] = {
    
    1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const LL INF = 1e15;
const int N = 3e5+10;
const LL Mod = 1234567891;
const int M = 2e6+10;
struct xx {
    
    
  int next, to; LL w;
  xx() {
    
    }
  xx(int next, int to, LL w) {
    
    
    this->next = next; this->to = to; this->w = w;
  }
}e[M];
int tot, head[N];
void Add(int u, int v, LL w) {
    
    
  e[++tot] = xx(head[u], v, w);
  head[u] = tot;
  e[++tot] = xx(head[v], u, w);
  head[v] = tot;
}
int s, t, u, v;
priority_queue<pli, vector<pli>, greater<pli> > q;
LL dis[N], vis[N];
void dij() {
    
    
  for(int i = 0; i <= t; ++i) dis[i] = INF, vis[i] = 0;
  dis[s] = 0; q.push(make_pair(dis[s], s));
  while(!q.empty()) {
    
    
    u = q.top().second; q.pop();
    if(vis[u]) continue;
    vis[u] = 1;
    for(int i = head[u]; i; i = e[i].next) {
    
    
      v = e[i].to;
      if(dis[v] > dis[u] + e[i].w) {
    
    
        dis[v] = dis[u] + e[i].w;
        q.push(make_pair(dis[v], v));
      } 
    }
  }
}
int n, m;
int main() {
    
    
  scanf("%d%d", &n, &m);
  int x, y; char dir; LL c;
  int u, v;
  s = 0; t = n*n+1;
  for(int i = 0; i < m; ++i) {
    
    
    scanf("%d %d %c %lld", &x, &y, &dir, &c);
    u = (x-1)*n + y;
    if(dir == 'R') v = max(s, u-n);
    else v = u, u = (y == n ? t : v + 1); 
    Add(u, v, c);
  }
  dij();
  if(dis[t] >= INF) puts("-1");
  else printf("%lld\n", dis[t]);
  return 0;
}

Guess you like

Origin blog.csdn.net/qq_43408978/article/details/109182847