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

让我看到你们的双手
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[N],dis[N],vis[N],cnt[N];
struct Edge
{
    int to,nxt,w;
}e[N];

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;  
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值