UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】

You are given a directed graph G(V,E) with a set of vertices and edges. Each edge (i,j) that connects some vertex i to vertex j has an integer cost associated with that edge.
Define the operation Halum(v,d) to operate on a vertex v using an integer d as follows: subtract d 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 two edges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2,−3) operates on edges 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 line represents 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
解题报告
该题的题意是,给定一个有向图,对于每一个点v可以进行一个操作,把以v为终点的边的边权减少d,把以v为起点的边的边权增加d。最后要求所有的边权非负且最大。
所谓“非负且最大”就要求这个有向图中的所有边权都大于一个最大的数,这个数我们可以通过二分答案来找。
我们令作用于点u上的所有操作d的和为sum[u],那么对于连接了u、v(u->v)两个点的边I,他的边权在进行了所有操作后就会是:ed[i].w+sum[u]-sum[v]。也就是说ed[i].w+sum[u]-sum[v]>=x,整理得sum[v]-sum[u]>=ed[I].w-x,很显然可以用最长路的的方法来做。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=500;
const int M=2700;
const int INF=1e9;
int head[N+5],num,dis[N+5],flag[N+5],cnt[N+5],n,m;
struct edge
{
    int v,w,next;
}e[M+5];
void add(int u,int v,int w)
{
    e[++num].v=v;
    e[num].w=w;
    e[num].next=head[u];
    head[u]=num;
}
bool spfa()
{
    memset(flag,0,sizeof(flag));
    memset(cnt,0,sizeof(cnt));
    queue<int>q;
    for(int i=0;i<=n;i++){dis[i]=0;q.push(i);}
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        flag[u]=false;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            if(dis[v]>dis[u]+e[i].w)//最长路 
            {
                dis[v]=dis[u]+e[i].w;
                if(!flag[v])
                {
                    if(++cnt[v]>n)return false;
                    flag[v]=true;
                    q.push(v);
                }
            }
        }
    }
    return true;
}
bool change(int x)
{
    for(int i=1;i<=n;i++)
    for(int j=head[i];j!=-1;j=e[j].next)
    e[j].w-=x;
    bool temp=spfa();
    for(int i=1;i<=n;i++)
    for(int j=head[i];j!=-1;j=e[j].next)
    e[j].w+=x;
    return temp;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,-1,sizeof(head));
        num=0;
        int lf=1,rg=0;
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
            rg=max(rg,w);
        }
        if(change(rg))
        {
            printf("Infinite\n");
            continue;
        }
        else if(!change(lf))
        {
            printf("No Solution\n");
            continue;
        }
        int ans=lf++;//这里是为了解决二分答案那个while循环进不去的输出问题 
        while(lf<rg)
        {
            int mid=(lf+rg)>>1;
            if(!change(mid))rg=mid;
            else        
            {
                lf=mid+1;
                ans=mid;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值