uva11478 Halum【二分+差分约束】

本文深入解析了信息技术领域的核心内容,包括但不限于前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具等多个细分领域。文章通过详细阐述各种关键技术、工具和实践案例,为读者提供了一个全面的技术视野,旨在提升对现代信息技术的理解和应用能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


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 InputSample Output  
 

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


Infinite
Infinite
3
1



  
 

Problem Setter: Md. Mahbubul Hasan
Next Generation Contest 5


WA了半个礼拜的题终于找到错在哪了==

先说题意:对于给定有向图,定义一种操作:对于选定的某一点,进入这个点的所有边权都减去d,从这个点出去的所有边权都加上d。经过一系列的操作,使得所有边权的最小值达到最大,需保证这个最值是正数==

既然是差分约束,需要记得把点当做最短路的点假设每个点都有一个点权sum(a),而w(a,b)+sum(a)-sum(b)>=x,x是所有边权的最小值,把边权sum放到等号右边,就构成了差分约束的不等式组,,保证不出现负环的spfa就是最大的x求得的。

再说我错在哪了,对于这个题来说,要人为加一个超级源点,(就像网络流一样,没有固定的源点汇点,就要人为添加)我就是因为没有这句话还觉得自己二分或者模板错了orz 要是不想加源点,spfa模板最开始push的点就是1-n都加进去,dist[]都是0

#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int inf=0x3f3f;
const int maxn=505;
int n,m;
struct Edge
{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
bool spfa(int start,int n)
{
    memset(vis,false,sizeof(vis));
    memset(dist,inf,sizeof(dist));
    vis[start]=true;
    dist[start]=0;
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(start);
    memset(cnt,0,sizeof(cnt));
    cnt[start]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            if(dist[v]>dist[u]+E[u][i].cost)
            {
                dist[v]=dist[u]+E[u][i].cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                    if(++cnt[v]>n) return false;
                }
            }
        }
    }
    return true;
}
bool judge(int x)
{
   // cout<<"rrr"<<endl;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<E[i].size();j++)
            E[i][j].cost-=x;
    }
    bool flag=spfa(0,n);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<E[i].size();j++)
            E[i][j].cost+=x;
    }
    return flag;
}
    int main()
    {
        //freopen("cin.txt","r",stdin);
       // int cas=1;
       // scanf("%d",&t);
        while(~scanf("%d%d",&n,&m))
        {
           // printf("Case #%d: ",cas++);
            for(int i=0;i<=n;i++) E[i].clear();
        //    memset(head,-1,sizeof(head));
            int l=1,mid,r=-inf;
            while(m--)
            {
                int a,b;
                int c;
                scanf("%d%d%d",&a,&b,&c);
                addedge(a,b,c);
                if(c>r)r=c;
            }
            for(int i=1;i<=n;i++)addedge(0,i,0);
          //  cout<<"rrr"<<endl;
           // printf("r=%d\n",r);
            if(judge(r))
            {
                printf("Infinite\n");
                continue;
            }
            else if(!judge(1))
            {
                puts("No Solution");
                continue;
            }
            int ans;
            while(l<=r)
            {
                mid=(l+r)/2;
               // cout<<mid<<endl;
               // printf("l=%d,mid=%d,r=%d    ",l,mid,r);
                if(judge(mid)) ans=mid,l=mid+1;
                else {r=mid-1;}
               // printf("l=%d,mid=%d,r=%d,ans=%d\n",l,mid,r,ans);
            }
            printf("%d\n",ans);
        }
        return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值