您的位置:首页 > 其它

差分约束(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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: