输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#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 4 2
3 1 0
1 2 -1
输出样例#1: 复制

Infinite
Infinite
3
1 最小值最大化-----> 二分答案转化为判定;
假设 i--->j 有一条权值为 wgt 的边,
那么我们操作后,假设此时我们要判定的答案为 mid;
∴ 应有 wgt + x[ i ]- x[ j ]>=mid;
其中 x[ i ] 表示在 i 点的操作值,
即: x[ j ]<=x[ i ]+ wgt - mid;
用差分约束解决;
判断是否有解判断是否存在环即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#pragma GCC optimize(2)
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, m;
int fm[maxn], To[maxn], wgt[maxn]; struct node {
int v, w;
node(){}
node(int v,int w):v(v),w(w){} }; vector<node>vc[maxn];
int dis[maxn];
bool vis[maxn], inQueue[maxn];
int flag; void spfa(int x) {
if (flag)return;
vis[x] = 1; inQueue[x] = 1;
int Siz = vc[x].size();
for (int i = 0; i < Siz; i++) {
int v = vc[x][i].v;
if (dis[v] > dis[x] + vc[x][i].w) {
dis[v] = dis[x] + vc[x][i].w;
if (!inQueue[v]) {
spfa(v);
}
else {
flag = 1; return;
}
}
}
inQueue[x] = 0;
} bool check(int x) {
flag = 0; ms(inQueue); ms(vis);
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; i++)vc[i].clear();
for (int i = 1; i <= m; i++) {
vc[fm[i]].push_back(node(To[i], wgt[i] - x));
}
for (int i = 1; i <= n; i++) {
if (vis[i])continue;
dis[i] = 0;
spfa(i);
if (flag)break;
}
if (flag)return false;
else return true;
} int main()
{
//ios::sync_with_stdio(0);
while (cin >> n >> m) {
for (int i = 1; i <= m; i++) {
rdint(fm[i]); rdint(To[i]); rdint(wgt[i]);
}
int l = 1, r = 1e5 + 1;
int ans = 0;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid))l = mid+1, ans = mid;
else r = mid - 1;
}
if (ans <= 0)cout << "No Solution" << endl;
else if (ans >= 50000)cout << "Infinite" << endl;
else cout << ans << endl;
}
return 0;
}

Halum UVA - 11478 差分约束的更多相关文章

  1. UVA 11478(差分约束 + 二分)

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

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

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

  3. UVA 515 差分约束 SPFA判负

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

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

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

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

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

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

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

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

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

  8. UVA 11478 Halum

    Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...

  9. 【Halum操作-UVA 11478】

    ·英文题,述大意:      输入有向图一个(什么边的端点啊,边权啊).每次可以选择一个节点和一个整数,然后把这个结点的出边边权加上该整数,入边边权减去该整数,目标:使得所有边的最小值非负且尽量大. ...

随机推荐

  1. 分布式爬虫搭建系列 之一------python安装及以及虚拟环境的配置及scrapy依赖库的安装

    python及scrapy框架依赖库的安装步骤: 第一步,python的安装 在Windows上安装Python 首先,根据你的Windows版本(64位还是32位)从Python的官方网站下载Pyt ...

  2. 分步编译一个C语言文件

    一. 以下是C程序一般的编译过程: 从图中看到: 将编写的一个c程序(源代码 )转换成可以在硬件上运行的程序(可执行代码 ),需要进行编译阶段  和链接这两个阶段. 其中, 1.  编译阶段先通过“编 ...

  3. Node.js的__dirname,__filename,process.cwd(),./的含义

    简单说一下这几个路径的意思,: __dirname: 获得当前执行文件所在目录的完整目录名 __filename: 获得当前执行文件的带有完整绝对路径的文件名 process.cwd():获得当前执行 ...

  4. 批处理基本知识以及进阶 V2.0

    批处理基本知识以及进阶 将以要执行的程序指令 , 像在 dos 模式下一下写入记事本 , 保存成 bat 文件 , 就可以执行了 一 . 简单批处理内部命令简介 1.Echo 命令 打开回显或关闭请求 ...

  5. iOS 本地加载js文件

    #import "RootViewController.h" @interface RootViewController ()<UIWebViewDelegate> @ ...

  6. linux所有文件中查找关键字的命令

     grep 192.168.1.1 * -r    在所有文件中查找192.168.1.1

  7. Arduino 003 Ubuntu(Linux) 系统下,如何给板子烧写程序

    Ubuntu/Linux 系统下,如何给Arduino板子烧写程序 使用的虚拟机软件:VMware 11 我的Ubuntu系统:Ubuntu 14.04.10 TLS Arduino 软件的版本:Ar ...

  8. java中字符串的存储

    在java中,不同的字符串赋值方法,其所在的地址可能不同也就导致,两个字符串的值看似相等可是在s1==s2操作时,其结果返回的却是false 例: String s1 = "Programm ...

  9. 914D Bash and a Tough Math Puzzle

    传送门 分析 用线段树维护区间gcd,每次查询找到第一个不是x倍数的点,如果这之后还有gcd不能被x整除的区间则这个区间不合法 代码 #include<iostream> #include ...

  10. 使用 classList API

    一.classList API 是什么 属于 DOM API,HTML5 引入,用来操作 HTML 标签的 class 属性值. classList 属性是一个只读的类数组对象,"实时&qu ...