【SDUTOJ 2414】 :An interesting game 网络流

传送门

题意

在这里插入图片描述

分析

这个数据范围很小,可以考虑用网络流处理
我们把 0 − 30 0 - 30 030这个范围内每一个值和每一个位置连边,流量为1,费用为该变量,然后设置虚拟原点控制流量,跑最小费用最大流即可

代码

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10,M = 1e6 + 10;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
    char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
    while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],ne[M],e[M],w[M],f[M],idx;
int n,m,S,T,k;
int q[N],d[N],pre[N],incf[N];
bool st[N];
int a[N];

void init(){
    for(int i = 0;i <= n + 100;i++) h[i] = -1,st[i] = 0,pre[i] = 0;
    idx = 0;
}

void add(int a,int b,int c,int d){
    ne[idx] = h[a],e[idx] = b,f[idx] = c,w[idx] = d,h[a] = idx++;
    ne[idx] = h[b],e[idx] = a,f[idx] = 0,w[idx] = -d,h[b] = idx++;
}

bool spfa(){
    int hh = 0,tt = 1;
    memset(d,0x3f,sizeof d);
    memset(incf,0,sizeof incf);
    q[0] = S,d[S] = 0,incf[S] = INF;
    while(hh != tt){
        int t = q[hh++];
        if(hh == N) hh = 0;
        st[t] = false;
        for(int i = h[t];~i;i = ne[i]){
            int ver = e[i]; 
            if(f[i] && d[ver] > d[t] + w[i]){
                pre[ver] = i;
                d[ver] = d[t] + w[i];
                incf[ver] = min(f[i],incf[t]);
                if(!st[ver]){
                    q[tt++] = ver;
                    if(tt == N) tt = 0;
                    st[ver] = true;
                }
            }
        }
    }
    return incf[T] > 0;
}

void EK(int &flow,int &cost){
    flow = cost = 0;
    while(spfa()){
        int t = incf[T];
        flow += t,cost += t * d[T];
        for(int i = T;i != S;i = e[pre[i] ^ 1]){
            f[pre[i]] -= t;
            f[pre[i] ^ 1] += t;
        }
    }
}

int main() {
    int num;
    read(num);
    for(int pppp = 1;pppp <= num;pppp++){
        printf("Case %d: ",pppp);
        read(n),read(m),read(k);
        init();
        for(int i = 1;i <= n;i++) read(a[i]);
        S = 0;
        int s = n + 51;
        T = n + 50;
        add(S,s,k,0);
        for(int i = 1;i <= m;i++) {
            int x;
            read(x);
            add(s,n + x + 1,1,0);
        }
        int ans = 0;
        for(int i = 1;i < n;i++){
            ans += abs(a[i + 1] - a[i]);
            for(int j = 0;j <= 30;j++){
                add(n + j + 1,i,1,abs(a[i + 1] - a[i]) - abs(j - a[i]) - abs(j - a[i + 1]));
            }
            add(i,T,1,0);
        }
        int flow,cost;
        EK(flow,cost);
        di(ans - cost);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Beijing, the capital city of China, is a fascinating place with a rich history and culture. It's a city that has something to offer for everyone, from foodies and shoppers to history buffs and nature lovers. However, there is one place in Beijing that stands out as the most interesting and captivating: The Forbidden City. The Forbidden City is a magnificent palace complex located in the heart of Beijing. It was once the home of the imperial family during the Ming and Qing dynasties, and today it's one of the most popular tourist attractions in China. The complex covers an area of 720,000 square meters and consists of more than 800 buildings, making it the largest palace complex in the world. The Forbidden City is surrounded by a moat and a 10-meter-high wall, which adds to its grandeur and mystique. The main entrance to the palace complex is through the Meridian Gate, which is guarded by two lion statues. Once inside, visitors are transported back in time to a world of imperial splendor and opulence. The palace complex is divided into two main sections, the Outer Court and the Inner Court. The Outer Court was used for ceremonial purposes and is where the emperor would receive officials and hold important ceremonies. The Inner Court was the private residence of the imperial family and is where the emperor and his family would live. One of the most impressive buildings in the Forbidden City is the Hall of Supreme Harmony, which is located in the Outer Court. This magnificent hall was used for important ceremonies such as the coronation of emperors and the proclamation of military victories. The hall is supported by 72 wooden columns, each made from a single tree, and the roof is adorned with intricate dragon and phoenix designs. Another must-see attraction in the Forbidden City is the Palace Museum, which is located in the Inner Court. The museum houses a vast collection of imperial treasures, including jade carvings, porcelain, and ancient calligraphy. Visitors can also see the living quarters of the imperial family, including the bedrooms, living rooms, and study rooms. Overall, the Forbidden City is a must-visit destination for anyone traveling to Beijing. It's a place that captures the essence of imperial China and offers a glimpse into the ancient world of the Ming and Qing dynasties. With its grandeur, history, and cultural significance, the Forbidden City is truly the most interesting place in Beijing.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值