差分约束(Halum操作,UVA 11478)
2017-02-11 18:39
495 查看
大白书上的题目翻译经常出错,比如这次大于0翻译成了非负。本来可以一次AC的东西多花了我2小时。
代码
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn = 550;
const int maxm = maxn*maxn;
struct Edge
{
int from,to,dist;
};
struct BellmanFord
{
int n,m;
vector<Edge>edges;
vector<int>G[maxn];
int d[maxn],inq[maxn],cnt[maxn];
void init(int n)
{
this->n=n;
edges.clear();
rep(i,0,n-1) G[i].clear();
}
void add(int u,int v,int dist)
{
edges.push_back((Edge){u,v,dist});
m=edges.size();
G[u].push_back(m-1);
}
bool circle()
{
queue<int>Q;
rep(i,0,n-1)
{
d[i]=cnt[i]=0;
inq[i]=1;
Q.push(i);
}
while(!Q.empty())
{
int u=Q.front();Q.pop();
inq[u]=0;
int sz=G[u].size();
rep(i,0,sz-1)
{
Edge& e=edges[G[u][i]];
if(d[e.to]>d[e.from]+e.dist)
{
d[e.to]=d[e.from]+e.dist;
if(!inq[e.to])
{
inq[e.to]=1;
Q.push(e.to);
if(++cnt[e.to]>n) return true;
}
}
}
}
return false;
}
};
int V,E;
int u[maxm],v[maxm],w[maxm];
BellmanFord BF;
const int MAX = 20002;
int main()
{
while(scanf("%d %d",&V,&E)==2)
{
rep(i,0,E-1)
{
scanf("%d %d %d",u+i,v+i,w+i);
u[i]--;
v[i]--;
}
int l=1,r=MAX;
while(l<r)
{
int m=l+(r-l)/2;
BF.init(V);
rep(i,0,E-1) BF.add(u[i],v[i],w[i]-m);
if(BF.circle()) r=m;
else l=m+1;
}
if(l==1) puts("No Solution");
else if(l==MAX) puts("Infinite");
else printf("%d\n",l-1);
}
return 0;
}
代码
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn = 550;
const int maxm = maxn*maxn;
struct Edge
{
int from,to,dist;
};
struct BellmanFord
{
int n,m;
vector<Edge>edges;
vector<int>G[maxn];
int d[maxn],inq[maxn],cnt[maxn];
void init(int n)
{
this->n=n;
edges.clear();
rep(i,0,n-1) G[i].clear();
}
void add(int u,int v,int dist)
{
edges.push_back((Edge){u,v,dist});
m=edges.size();
G[u].push_back(m-1);
}
bool circle()
{
queue<int>Q;
rep(i,0,n-1)
{
d[i]=cnt[i]=0;
inq[i]=1;
Q.push(i);
}
while(!Q.empty())
{
int u=Q.front();Q.pop();
inq[u]=0;
int sz=G[u].size();
rep(i,0,sz-1)
{
Edge& e=edges[G[u][i]];
if(d[e.to]>d[e.from]+e.dist)
{
d[e.to]=d[e.from]+e.dist;
if(!inq[e.to])
{
inq[e.to]=1;
Q.push(e.to);
if(++cnt[e.to]>n) return true;
}
}
}
}
return false;
}
};
int V,E;
int u[maxm],v[maxm],w[maxm];
BellmanFord BF;
const int MAX = 20002;
int main()
{
while(scanf("%d %d",&V,&E)==2)
{
rep(i,0,E-1)
{
scanf("%d %d %d",u+i,v+i,w+i);
u[i]--;
v[i]--;
}
int l=1,r=MAX;
while(l<r)
{
int m=l+(r-l)/2;
BF.init(V);
rep(i,0,E-1) BF.add(u[i],v[i],w[i]-m);
if(BF.circle()) r=m;
else l=m+1;
}
if(l==1) puts("No Solution");
else if(l==MAX) puts("Infinite");
else printf("%d\n",l-1);
}
return 0;
}
相关文章推荐
- UVA 11478 Halum (差分约束)
- UVA 11478 Halum(差分约束,5级)
- UVa 11478 Halum (差分约束)
- UVA11478 Halum (差分约束)
- UVA 11478 Halum(差分约束 SPFA判负环)
- uva11478 Halum【二分+差分约束】
- UVA 11478 Halum(差分约束,5级)
- UVA 11478 Halum(二分 + 差分约束)
- uva 11478 Halum(图论-差分约束)
- UVA 11478 Halum(差分约束)
- UVA 11478 Halum(差分约束)
- UVA11478 Halum (差分约束)
- uva 11478 Halum(图论-差分约束)
- Uva 11478 Halum操作
- 【Halum操作-UVA 11478】
- UVA 11478 Halum(用bellman-ford解差分约束)
- 例题5.16 Halum操作 UVa11478
- UVA 11478(差分约束 + 二分)
- [UVA]11478 二分答案+差分约束
- 11478 - Halum(差分约束)