题意:

给定一个有向图,每条边都有一个权值,每次你可以选择一个结点和一个整数的,把所有以v为终点的边的权值减去d,

把所有以v为起点的边的权值加上d

最后要让所有边的权的最小值非负且尽量大

代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e3;
const int inf = 0x3f3f3f3f;
int dist[N],inq[N],cnt[N];
struct node
{
int v,w;
node(int v,int w):v(v),w(w) {};
};
vector<node> G[N];
void init(int n)
{
for(int i = 0; i<=n; i++) G[i].clear();
}
bool spfa(int n)
{
queue<int> q;
q.push(0);
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
memset(dist,inf,sizeof(dist));
dist[0] = 0;
inq[0] = 1;
cnt[0] = 1;
while(!q.empty())
{
int u = q.front();
q.pop();
inq[u] = 0;
for(int i = 0; i<G[u].size(); i++)
{
int v = G[u][i].v, w= G[u][i].w;
if(dist[v]>dist[u] + w)
{
dist[v] = dist[u] + w;
if(!inq[v]) q.push(v),inq[v] = 1;
if(++cnt[v]>n) return true;
}
}
}
return false;
}
bool test(int mid,int n)
{
for(int i = 1; i<=n; i++)
for(int j = 0; j<G[i].size(); j++)
G[i][j].w -=mid;
int ret = spfa(n);
for(int i = 1; i<=n; i++)
for(int j = 0; j<G[i].size(); j++)
G[i][j].w +=mid;
return ret;
}
int main()
{
int V,E;
while(~scanf("%d%d",&V,&E))
{
init(V);
int ub = 0;
int u ,v ,w;
for(int i = 0; i<E; i++)
{
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(node(v,w));
ub = max(w,ub);
}
for(int i = 1; i<=V; i++)
G[0].push_back(node(i,0));
int L = 1, R = ub;
if(test(L,V)) puts("No Solution");
else if(!test(ub+1,V)) puts("Infinite");
else
{
while(L<R)
{
int mid = L + (R -L)/2;
if(test(mid,V)) R = mid ;
else L = mid + 1;
}
printf("%d\n",L-1);
}
}
return 0;
}

UVA 11478(差分约束 + 二分)的更多相关文章

  1. Halum UVA - 11478 差分约束

    输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 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 ...

  2. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

  3. POJ 1364 King (UVA 515) 差分约束

    http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  4. hdu1529 Cashier Employment[差分约束+二分答案]

    这题是一个类似于区间选点,但是有一些不等式有三个未知量参与的情况. 依题意,套路性的,将小时数向右平移1个单位后,设$f_i$为前$i$小时工作的人数最少是多少,$f_{24}$即为所求.设$c_i$ ...

  5. UVA 515 差分约束 SPFA判负

    第一次看这个题目,完全不知道怎么做,看起来又像是可以建个图进行搜索,但题目条件就给了你几个不等式,这是怎么个做法...之后google了下才知道还有个差分约束这样的东西,能够把不等式化成图,要求某个点 ...

  6. Halum UVA - 11478(差分约束 + 二分最小值最大化)

    题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...

  7. poj 1275 Cashier Employment - 差分约束 - 二分答案

    A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its n ...

  8. 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)

    layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...

  9. UVA 11478 Halum(用bellman-ford解差分约束)

    对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...

随机推荐

  1. Linux平台下卸载MySQL的方法

    转载自: https://www.cnblogs.com/taomylife/p/7234925.html Linux平台下卸载MySQL的方法: MySQL的安装主要有三种方式:二进制包安装.RPM ...

  2. CentOS 6.5通过yum安装 MySQL-5.5

    1.安装mysql-5.5的yum源 rpm -ivh http://repo.mysql.com/yum/mysql-5.5-community/el/6/x86_64/mysql-communit ...

  3. GNU汇编 伪指令

    伪指令 本身并没有所对应的机器码 它只是在编译的时候起作用,或者转换为其他的实际指令来运行 global ascii byte word data equ align @ 下面的例子是在数据段存放数据 ...

  4. kali安装ssh服务

    一. kali安装ssh服务 1.修改源 root@DGG:~# vi /etc/apt/sources.list deb http://http.kali.org/kali kali-rolling ...

  5. 初见spark-04(高级算子)

    今天,这个是spark的高级算子的讲解的最后一个章节,今天我们来介绍几个简单的算子, countByKey val rdd1 = sc.parallelize(List(("a", ...

  6. Sqoop 工具使用

    Sqoop 是什么及安装 Hadoop sqoop Apache sqoop (SQL to Hadoop) Sqoop is a tool designed to transfer data bet ...

  7. 13,SQLAlchemy 增删改查 一对多 多对多

    今天来聊一聊 Python 的 ORM 框架 SQLAlchemy Models 是配置和使用比较简单,因为他是Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLA ...

  8. 十、mysql之索引原理与慢查询优化

    mysql之索引原理与慢查询优化 一.介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还 ...

  9. MVC WebAPI 的基本使用

    1.什么是WebAPI Web API是网络应用程序接口.包含了广泛的功能,网络应用通过API接口,可以实现存储服务.消息服务.计算服务等能力,利用这些能力可以进行开发出强大功能的web应用. 它可以 ...

  10. RelativeLayout 深入理解

    今天做app底部的导航栏,就是会有一个分割线,分割内容和下面的fragmenttablehost,那条线,我看开源中国是用relativelayout包裹的. 我也包裹,但是不行.显示不出来那条线. ...