输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#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. 发RTX通知

    安装sdk 在RTXServer目录下找到WebRoot目录,找到里面的SendNotify.cgi(就是一个php页面,默认是pc - ascii编码).打开页面,在头部加上编码信息 header( ...

  2. Python多进程-进程间数据的共享

    不同的进程不能同时修改一份数据,但是不同的进程能对一份数据进行修改 可通过Manager来实现进程间的数据共享 # -*- coding:utf-8 -*- __author__ = "Mu ...

  3. 有关Backgroundworker

    (一)Backgroundworker取消时应该用的有关代码: CancelAsync方法是在前台主线程用的,CancellationPending属性是在后台子线程用的.实际的使用方式应该是这样的: ...

  4. 12-15winform--窗体

    一.窗体:有许多控件组成,空间本身就是类对象: 1)每一个控件都有自己的属性和方法. 2)控件的方法叫做事件: 3)同一命名空间下的项目文件是一个整体文件.设计器的代码(类文件)在“解决方案管理器”- ...

  5. javaScript之动态样式

        动态添加样式可以实现更好的视觉效果和交互效果,下面就介绍一下如何动态和删除样式: 方法一.使用obj.className来修改样式表的类名 obj.className = “style1”; ...

  6. Eclipse 快键键(持续更新)

    本人抛弃一些简单常见的快键键,例如 ctrl+c   ,+v ,+z之类的 1.ctrl+d 删除一整行 2.ctrl+f 搜索 3.光标选中几行,ctrl+alt+↓ 向下复制选中的那几行 4.光标 ...

  7. Ubuntu安装Chrome及hosts修改

    Ubuntu16.04 1.chrome安装 获取安装包http://www.google.cn/chrome/browser/desktop/index.html 在安装包目录打开终端执行sudo  ...

  8. Android指针管理:RefBase,SP,WP (二)

    (1)在Android中,RefBase结合了sp和wp,实现了一套通过引用计数的方法来控制对象声明周期的方法. RefBase的定义在/frameworks/base/include/utils/R ...

  9. 第四天:servlet的生命周期和一些细节问题

    1.  servlet的生命周期: a)  流程 i.  Web服务器首先会检查是否装载了该servlet的实例对象.如果装载了直接进行第四步. ii.  装载并创建该servlet的实例对象. ii ...

  10. Lucene源码解析--Analyzer之Tokenizer

    Analyzer包含两个核心组件,Tokenizer以及TokenFilter.两者的区别在于,前者在字符级别处理流,而后者则在词语级别处理流.Tokenizer是Analyzer的第一步,其构造函数 ...