G - THE MATRIX PROBLEM(差分约束&判负环优化)
作者:互联网
G - THE MATRIX PROBLEM(差分约束&判负环优化)
然后转差分约束判负环即可,最好建立一个超级源点,避免图不连通。
本题需要判负环的时候优化,即: + + i n [ v ] > s q r t ( n + m ) ++in[v]>sqrt(n+m) ++in[v]>sqrt(n+m) 返回 f a l s e false false。
// Problem: G - THE MATRIX PROBLEM
// Contest: Virtual Judge - 2010icpc [Cloned]
// URL: https://vjudge.net/contest/467412#problem/G
// Memory Limit: 32 MB
// Time Limit: 2000 ms
// Date: 2021-11-07 20:11:34
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e3+5,M=2e6+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
#define db double
db lgL,lgR;
int h[N],cnt,n,m,L,R;
db d[N];
struct edge{
int to,nt;
db w;
}e[M<<1];
void add(int u,int v,db w){
e[++cnt]={v,h[u],w},h[u]=cnt;
}
bitset<N>vis;
int in[N];
void bud(){
for(int i=1;i<=n+m;i++) add(0,i,0);
rep(i,1,n){
rep(j,1,m){
int x;scanf("%d",&x);
db lgx=log(1.0*x);
add(n+j,i,lgR-lgx);
add(i,n+j,lgx-lgL);
}
}
}
int sqk;
bool spfa(int st,int ed){
mst(in,0);vis.reset();
queue<int>q;
rep(i,1,n+m) d[i]=inf;
d[0]=0;
vis[st]=1;q.push(st);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=0;
for(int i=h[u];i;i=e[i].nt){
int v=e[i].to;
db w=e[i].w;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]) {
if(++in[v]>sqk) return false;
q.push(v),vis[v]=1;
}
}
}
}
return true;
}
int main(){
while(~scanf("%d%d%d%d",&n,&m,&L,&R)){
cnt=0;mst(h,0);
lgL=log(L),lgR=log(R);
sqk=sqrt(n+m);
bud();
puts(spfa(0,n+m)?"YES":"NO");
}
return 0;
}
标签:MATRIX,vis,int,++,long,判负,PROBLEM,define 来源: https://blog.csdn.net/weixin_45750972/article/details/121196926