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;
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12686534.html

时间: 2024-10-09 20:09:19

The 2019 China Collegiate Programming Contest Harbin Site A - Artful Paintings 差分约束的相关文章

The 2019 China Collegiate Programming Contest Harbin Site

目录 Contest Info Solutions A. Artful Paintings E. Exchanging Gifts F. Fixing Banners I. Interesting Permutation J. Justifying the Conjecture K. Keeping Rabbits L. LRU Algorithm Contest Info Practice Link Solved A B C D E F G H I J K L 6/12 O - - - O O

The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners

链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word meaning "a place for drying fishing nets", grew from a small rural settlement on the Songhua River to become one of the largest cities in Northea

The 2019 China Collegiate Programming Contest Harbin Site K. Keeping Rabbits

链接: https://codeforces.com/gym/102394/problem/K 题意: DreamGrid is the keeper of n rabbits. Initially, the i-th (1≤i≤n) rabbit has a weight of wi. Every morning, DreamGrid gives the rabbits a carrot of weight 1 and the rabbits fight for the only carrot

The 2019 China Collegiate Programming Contest Harbin Site J. Justifying the Conjecture

链接: https://codeforces.com/gym/102394/problem/J 题意: The great mathematician DreamGrid proposes a conjecture, which states that: Every positive integer can be expressed as the sum of a prime number and a composite number. DreamGrid can't justify his c

The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation

链接: https://codeforces.com/gym/102394/problem/I 题意: DreamGrid has an interesting permutation of 1,2,-,n denoted by a1,a2,-,an. He generates three sequences f, g and h, all of length n, according to the permutation a in the way described below: For ea

The 2019 China Collegiate Programming Contest Harbin Site I - Interesting Permutation 思维

//#include<bits/stdc++.h> #include<map> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define int long long using namespace std; #define rep_1(i,m,n) for(int i=m;i<=n

The 2019 China Collegiate Programming Contest Harbin Site E - Exchanging Gifts 拓扑图+离散化

非常难受的是,我用链表写的,要么wa,要么tle,甚至还出现了超内存... 然后换成矩阵,开始还是wa了两次,然后换了别的快读,才过,难受. #include<map> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; #

2019 China Collegiate Programming Contest Qinhuangdao Onsite

目录 Contest Info Solutions A. Angle Beats D. Decimal F. Forest Program I. Invoker J. MUV LUV EXTRA Contest Info Practice Link Solved A B C D E F G H I J K L 5/12 O - - O - O - - - O ? - O 在比赛中通过 ? 赛后通过 ! 尝试了但是失败了 - 没有尝试 Solutions A. Angle Beats 题意: 给出

2018 China Collegiate Programming Contest Final (CCPC-Final 2018)(A B G I L)

A:签到题,正常模拟即可. 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5 + 5; 4 struct node{ 5 int id, time; 6 }; 7 node a[maxn]; 8 bool cmp(const node &a, const node &b){ 9 if(a.id^b.id) return a.id < b.id; 10 else return a.