您的位置:首页 > 其它

UVA 11478 查分约束+二分答案 解题报告

2017-07-10 20:17 411 查看
让我看到你们的双手

Halum

Description

You are given a directed graph G(V; E) with a set of vertices and edges. Each edge (i; j) that connectssome vertex i to vertex j has an integer cost associated with that edge.

De ne the operation Halum(v; d) to operate on a vertex v using an integer d as follows: subtractd from the cost of all edges that enter v and add d to the cost of every edge that leaves v.

As an example of that operation, consider graph G that has three vertices named (1, 2, 3) and twoedges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2; 3) operates onedges entering and leaving vertex 2. Thus, edge (1, 2) gets cost -1-(-3)=2 and the edge (2, 3) gets cost

1 + (-3) = -2.

Your goal is to apply the Halum function to a graph, potentially repeatedly, until every edge in the

graph has at least a certain cost that is greater than zero. You have to maximize this cost.

Input

Two space-separated integers per case: V (V  500) and E (E  2700). E lines follow. Each linerepresents a directed edge using three space-separated integers (u; v; d). Absolute value of cost can be

at most 10000.

Output

If the problem is solvable, then print the maximum possible value. If there is no such solution print‘No Solution’. If the value can be arbitrary large print `Infinite’

Sample Input

2 1

1 2 10

2 1

1 2 -10

3 3

1 2 4

2 3 2

3 1 5

4 5

2 3 4

4 2 5

3 4 2

3 1 0

1 2 -1

Sample Output

Infinite

Infinite

3

1

【解题报告】

卡了好久,身心疲惫,烦躁异常,什么也不想说。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 27005
#define inf 0x3f3f3f3f

int n,m,num;
int head
,dis
,vis
,cnt
;
struct Edge
{
int to,nxt,w;
}e
;

void adde(int u,int v,int w)
{
e[num].w=w;
e[num].to=v;
e[num].nxt=head[u];
head[u]=num++;
}
bool SPFA()
{
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));
memset(cnt,0,sizeof(cnt));
queue<int> q;
q.push(0);
vis[0]=cnt[0]=1;dis[0]=0;
while(!q.empty())
{
int now=q.front();q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].nxt)
{
int id=e[i].to;
if(dis[id]>dis[now]+e[i].w)
{
dis[id]=dis[now]+e[i].w;
if(!vis[id])
{
if(++cnt[id]>n) return false;
vis[id]=1;
q.push(id);
}
}
}
}
return true;
}
bool check(int x)
{
for(int i=1;i<=n;++i)
for(int j=head[i];~j;j=e[j].nxt) e[j].w-=x;
bool flag=SPFA();
for(int i=1;i<=n;++i)
for(int j=head[i];~j;j=e[j].nxt) e[j].w+=x;
return flag;
}

int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-1,sizeof(head));
num=0;
for(int i=1;i<=n;++i) adde(0,i,0);
int L=1,R=0;
for(int i=1;i<=m;++i)
{
int from,to,val;
scanf("%d%d%d",&from,&to,&val);
adde(from,to,val);
if(val>R) R=val;
}
if(check(R)) {puts("Infinite");continue;}
else if(!check(L)) {puts("No Solution");continue;}
int ans=L++;
while(L<R)
{
int mid=L+((R-L)>>1);
if(!check(mid)) R=mid;
else
{
L=mid+1;
ans=mid;
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: