The 2019 China Collegiate Programming Contest Harbin Site A - Artful Paintings 差分约束
Posted qingyuyyyyy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了The 2019 China Collegiate Programming Contest Harbin Site A - Artful Paintings 差分约束相关的知识,希望对你有一定的参考价值。
#include<map>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int N=1e5+5;
const int M=1e5+5;
const int INF=0x3f3f3f3f;
int read()
{
int res=0,ch,flag=0;
if((ch=getchar())==‘-‘) //判断正负
flag=1;
else if(ch>=‘0‘&&ch<=‘9‘) //得到完整的数
res=ch-‘0‘;
while((ch=getchar())>=‘0‘&&ch<=‘9‘)
res=res*10+ch-‘0‘;
return flag?-res:res;
}
int t;
int n,M1,M2;
int e[N],h[N],ne[N],w[N],idx;
int l1[N],r1[N],k1[N];
int l2[N],r2[N],k2[N];
inline void add(int u,int v,int c)
{
e[idx]=v;
w[idx]=c;
ne[idx]=h[u];
h[u]=idx++;
}
int dis[N];
bool vis[N];
inline bool spfa(int mid)
{
queue<int> q;
idx=0;
memset(h,-1,sizeof h);
memset(dis,-1,sizeof dis);
memset(vis,0,sizeof vis);
bool flag=0;
for(int i=1; i<=n; i++)
{
add(i-1,i,0);
add(i,i-1,-1);
}
//l r 不少于k1
//l-1 -> r k1
for(int i=1; i<=M1; i++)
add(l2[i]-1,r2[i],k2[i]);
//l r 之外的 不少于k2
//也就是 l r 之间 <=mid-k2
//改成相同的就是
//r -> l-1 mid-k2
for(int i=1; i<=M2; i++)
add(r1[i],l1[i]-1,k1[i]-mid);
add(0,n,mid);
add(n,0,-mid);
dis[0]=0;
q.push(0);
vis[0]=1;
while(!q.empty()&& !flag)
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=h[u]; (~i)&&!flag; i=ne[i])
{
if(dis[e[i]]<dis[u]+w[i])
{
dis[e[i]]=dis[u]+w[i];
if(dis[0]>0)
flag=1;
if(!vis[e[i]])
{
q.push(e[i]);
vis[e[i]]=1;
}
}
}
}
return !flag;
}
int main()
{
t=read();
while(t--)
{
n=read(),M1=read(),M2=read();
int l=0,r=n,mid,ans=n;
for(int i=1; i<=M1; i++)
l2[i]=read(),r2[i]=read(),k2[i]=read();
for(int i=1; i<=M2; i++)
l1[i]=read(),r1[i]=read(),k1[i]=read();
while(l<r)
{
mid=(l+r)>>1;
if(spfa(mid))
r=mid;
else
l=mid+1;
}
cout<<r<<endl;
}
return 0;
}
以上是关于The 2019 China Collegiate Programming Contest Harbin Site A - Artful Paintings 差分约束的主要内容,如果未能解决你的问题,请参考以下文章
The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners
The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation
The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits
The 2019 China Collegiate Programming Contest Harbin Site I - Interesting Permutation 思维
The 2019 China Collegiate Programming Contest Harbin Site A - Artful Paintings 差分约束
Rolling The Polygon (2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest)(The