输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#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. 0ctf签到题

    签到题: 题目提示: flag在IRC聊天室里 开启weechat ┌─[root@sch01ar]─[~] └──╼ #weechat 创建服务器 /server add freenode8001 ...

  2. java 多线程系列---JUC原子类(三)之AtomicLongArray原子类

    AtomicLongArray介绍和函数列表 在"Java多线程系列--“JUC原子类”02之 AtomicLong原子类"中介绍过,AtomicLong是作用是对长整形进行原子操 ...

  3. [Python Study Notes]物体运动检测

    基于opencv的cv2模块实现 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. spring配置c3p0连接池

  5. oracle -sqlplus 调用存储过程

    sp.batsqlplus user/passwd/sid  @.\sp.sql >sp.sqlexit; sp.sqlexex xxxx()quit;

  6. day35 02-Hibernate持久化对象状态及状态转换

    hibernate内置有一个c3p0,不用引入c3p0的jar包也行. 现在其实可以不用去创建表和实体类.因为hibernate可以自动帮我们生成.只要把映射建好了它就可以自动帮我们生成. 创建实体类 ...

  7. ArcGIS10拓扑规则-面规则(转)

    ArcGIS10拓扑规则-面规则 原创 2013年12月27日 10:20:44 标签: ArcGIS 1879 ARCGIS 10 里提供的拓扑规则共32种,下面一一介绍: 首先介绍的对于面图层的拓 ...

  8. Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)

    题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...

  9. 【转】nginx禁止访问某个文件和目录(文件夹)

    nginx禁止访问所有.开头的隐藏文件设置 location ~* /.* {deny all;} nginx禁止访问目录, 例如:禁止访问path目录 location ^~ /path {deny ...

  10. Node.js 介绍及学习

    Node.js => 简单来理解,就是指运行在服务器端的JavaScript.  Node.js 是一个基于Chrome JavaScript运行时建立的一个平台 Node.js是一个事件驱动I ...