OJ 1190

二分答案,对每个答案进行判断,用并查集进行优化,将整个场地的左侧和右侧分别设为0和n+1,每个点覆盖的范围设为i,$O(n^2)$判断点之间是否连通,最后只需判断0和n+1是否连通即可。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

inline long long read() {
    long long ans = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
        f *= (ch == '-') ? -1 : 1, ch = getchar();
    do ans = ((ans << 1) + (ans << 3) + (ch ^ 48)), ch = getchar();
    while(ch >= '0' && ch <= '9');
    return ans * f;
}

const int MAXN = 2001;
const double esp = 1e-7;
double a[MAXN], b[MAXN], x, y;
int fa[MAXN], n;

inline int get(int x){
    return (fa[x] == x) ? x : fa[x] = get(fa[x]);
}

bool judge(double val){
    for(int i=0; i<=n+1; i++)
        fa[i] = i;
    for(int i=1; i<=n; i++){
        if(a[i] < val + esp || b[i] + val + esp > y) fa[get(i)] = get(0);
        if(b[i] < val + esp || a[i] + val + esp > x) fa[get(i)] = get(n+1);
    }
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(i != j && (a[j] - a[i]) * (a[j] - a[i]) + (b[j] - b[i]) * (b[j] - b[i]) < val * val * 4 + esp)
                fa[get(i)] = get(j);
    return get(0) != get(n+1);
}

int main(){
    x = read(), y = read(), n = read();
    for(int i=1; i<=n; i++)
        a[i] = read(), b[i] = read();
    double l = 0, r = max(x, y);
    while(l + 1e-3 < r){
        double mid = (l + r) / 2;
        if(judge(mid)) l = mid;
        else r = mid;
    }
    printf("%.2lf", l);
    return 0;
}
View Code

OJ 1191

整个图是一个基环树,要求基环树直径,分为两种情况,一个是路径经过环,一个路径不经过环,第二种情况直接Dp求直径,对第一种情况使用动态规划

$ans = max(ans, Dp[i]+Dp[j]+sum[i]-sum[j]),i>j-len,sum[i]-sum[j]<=sum[len]/2$,单调队列优化即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long read(){
    long long ans = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
        f *= (ch == '-') ? -1 : 1, ch = getchar();
    do ans = ((ans << 1) + (ans << 3) + (ch ^ 48)), ch = getchar();
    while(ch >= '0' && ch <= '9');
    return ans * f;
}

const int MAXN = 1000005;
int head[MAXN], next[MAXN<<1], last[MAXN<<1], val[MAXN<<1], lineNum;
void add(int x,int y,int v){
    lineNum++, next[lineNum] = head[x], last[lineNum] = y, val[lineNum] = v, head[x] = lineNum;
}
int cir[MAXN<<1], top, len, sta[MAXN], vis[MAXN];
long long ans, ret, Dp[MAXN], ext[MAXN<<1];
int deq[MAXN<<1], L = 0, R = -1;

struct Edge{
    int x, y, val;
    const bool operator < (const Edge &temp) const{
        if(x != temp.x) return x < temp.x;
        if(y != temp.y) return y < temp.y;
        return val < temp.val;
    }
}e[MAXN];

void findCir(int x,int fa){
    sta[++top] = x, vis[x] = 1;
    for(int l = head[x]; l; l = next[l]){
        int y = last[l];
        if(y == fa) continue;
        if(vis[y] == 1){
            do cir[++len] = sta[top], vis[sta[top]] = 2;
            while(sta[top--] != y);
            continue;
        }
        if(!vis[y]) findCir(y, x);
    }
    if(vis[x] == 1)
        vis[x] = 2, top--;
}

void dfs(int x,int fa){
    for(int l = head[x]; l; l = next[l]){
        int y = last[l];
        if(y == fa || vis[y]) continue;
        dfs(y, x);
        ret = max(ret, Dp[x] + Dp[y] + val[l]);
        Dp[x] = max(Dp[x], Dp[y] + val[l]);
    }
}

int main(){
    int n = read();
    for(int i=1; i<=n; i++){
        e[i].x = read(), e[i].y = read(), e[i].val = read();
        if(e[i].x > e[i].y) swap(e[i].x, e[i].y);
    }
    sort(e+1, e+1+n);
    bool flag = false;
    for(int i=1; i<=n; i++){
        if(e[i].x == e[i].y) flag = true;
        else if(e[i].x == e[i-1].x && e[i].y == e[i-1].y) flag = true;
        else add(e[i].x, e[i].y, e[i].val), add(e[i].y, e[i].x, e[i].val);
    }
    if(flag){
        dfs(1, 0);
        printf("%lld", ret + 1);
        return 0;
    }
    findCir(1, -1);
    cir[0] = cir[len];
    for(int i=len+1; i<=len*2; i++)
        cir[i] = cir[i-len];
    for(int i=1; i<=len; i++)
        for(int l = head[cir[i]]; l; l = next[l])
            if(last[l] == cir[i-1])
                ext[i] = val[l];
    for(int i=len+1; i<=len*2; i++)
        ext[i] = ext[i-len];
    for(int i=1; i<=len*2; i++)
        ext[i] += ext[i-1];
    memset(vis, 0, sizeof(vis));
    for(int i=1; i<=len; i++){
        vis[cir[i-1]] = vis[cir[i+1]] = true;
        ret = 0, dfs(cir[i], -1);
        vis[cir[i-1]] = vis[cir[i+1]] = false;
        ans = max(ans, ret);
    }
    for(int i=1; i<=len*2; i++){
        while(L <= R && (deq[L] < i - len || ext[i] - ext[deq[L]] > ext[len] / 2))
            L++;
        if(L <= R)
            ans = max(ans, Dp[cir[i]] + Dp[cir[deq[L]]] + ext[i] - ext[deq[L]]);
        while(L <= R && Dp[cir[deq[R]]] - ext[deq[R]] <= Dp[cir[i]] - ext[i])
            R--;
        deq[++R] = i;
    }
    printf("%lld", ans + 1);
    return 0;
}
View Code

OJ 1192

使用树形Dp,Dp[MAXN][2],第二维表示以x为根的子树使用(不使用)传送阵的最短时间,转移方程:

$Dp[x][0] += Dp[y][0] + 2*val[l],Dp[x][1] += min(Dp[y][1] + 2*val[l], Dp[y][0] + val[l] - d[y])$

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

long long read(){
    long long ans = 0, f = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
        f *= (ch == '-') ? -1 : 1, ch = getchar();
    do ans = ((ans << 1) + (ans << 3) + (ch ^ 48)), ch = getchar();
    while(ch >= '0' && ch <= '9');
    return ans * f;
}

const int MAXN = 1000001;
int head[MAXN], next[MAXN<<1], last[MAXN<<1], val[MAXN<<1], lineNum;
void add(int x,int y,int v){
    lineNum++, next[lineNum] = head[x], last[lineNum] = y, val[lineNum] = v, head[x] = lineNum;
}
long long Dp[MAXN][2], d[MAXN];

void dfs(int x,int fa){
    for(int l = head[x]; l; l = next[l]){
        int y = last[l];
        if(y == fa) continue;
        dfs(y, x);
        d[x] = max(d[x], d[y] + val[l]);
        Dp[x][0] += Dp[y][0] + val[l] * 2;
        Dp[x][1] += min(Dp[y][1] + val[l] * 2, Dp[y][0] + val[l] - d[y]);
    }
}

int main(){
    int n = read();
    for(int i=1; i<n; i++){
        int x = read(), y = read(), v = read();
        add(x, y, v), add(y, x, v);
    }
    dfs(1, -1);
    printf("%lld", Dp[1][1]);
    return 0;
} 
View Code

 

posted on 2020-02-29 16:09  PHDHD  阅读(144)  评论(0编辑  收藏  举报