UVA 11478 Halum
Halum
This problem will be judged on UVA. Original ID: 11478
64-bit integer IO format: %lld Java class name: Main
You are given a directed graph G(V,E) with a set of vertices and edges. Each edge (i,j) that connects some vertex i to vertex j has an integer cost associated with that edge.
Define the operation Halum(v, d) to operate on a vertex v using an integer d as follows: subtract d 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 two edges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2,-3) operates on edges 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 line represents 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 <deque>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = ;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
int head[maxn],tot,n,m;
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
int d[maxn],cnt[maxn];
bool in[maxn];
bool spfa(int x) {
deque<int>q;
for(int i = ; i <= n; ++i) {
cnt[i] = ;
d[i] = ;
in[i] = true;
q.push_back(i);
}
while(!q.empty()) {
int u = q.front();
q.pop_front();
in[u] = false;
for(int i = head[u]; ~i; i = e[i].next) {
int tmp = e[i].w - x;
if(d[e[i].to] > d[u] + tmp) {
d[e[i].to] = d[u] + tmp;
if(!in[e[i].to]) {
if(++cnt[e[i].to] > n) return false;
in[e[i].to] = true;
if(!q.empty() && d[q.front()] > d[e[i].to])
q.push_front(e[i].to);
else q.push_back(e[i].to);
}
}
}
}
return true;
}
int main() {
int u,v,w;
while(~scanf("%d%d",&n,&m)) {
memset(head,-,sizeof head);
int low = ,high = ;
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
high = max(high,w);
}
if(!spfa()) puts("No Solution");
else if(spfa(high+)) puts("Infinite");
else {
int ret;
while(low <= high) {
int mid = (low + high)>>;
if(spfa(mid)) {
ret = mid;
low = mid+;
} else high = mid - ;
}
printf("%d\n",ret);
}
}
return ;
}
UVA 11478 Halum的更多相关文章
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- UVA 11478 Halum (差分约束)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 11478 Halum操作
题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...
- UVA - 11478 Halum 二分+差分约束
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 【Halum操作-UVA 11478】
·英文题,述大意: 输入有向图一个(什么边的端点啊,边权啊).每次可以选择一个节点和一个整数,然后把这个结点的出边边权加上该整数,入边边权减去该整数,目标:使得所有边的最小值非负且尽量大. ...
- Halum UVA - 11478(差分约束 + 二分最小值最大化)
题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...
随机推荐
- ongl表达式中得到对象,调用对象方法(OA项目权限显示模块)
在用户是否拥有某项权限的问题 是这样解决的: 用户登录之后 登录信息是保存在session域中的 通过el表达式可得到登录的对象信息 那么怎样判断用户是否拥有某项权限呢 ?如果没有上图中的判断 ...
- Makefile错误总结
自己在做嵌入式驱动时,编写makefile文件是犯的错及解决办法 问题1:makefile 3 missing separator.stop: 问题2:Nothing to be done for ' ...
- nodejs-路由(待补充)
path Router 1 2 3 4 5 var express = require('express'); var Router = express.Router(); Router.get('/ ...
- SQL-Oracle-创建Dblink
create database link DBLINK_IMARK_RAC connect to imark identified by imarkDB12345 using '(DESCRIPTIO ...
- Java Pattern Matcher 正则表达式需要转义的字符
见:http://blog.csdn.net/bbirdsky/article/details/45368709 /** * 转义正则特殊字符 ($()*+.[]?\^{},|) * * @param ...
- Mysql存储过程包括事务,且传入sql数据运行
有这样一个需求.要求在mysql存储过程中使用到事务,并且运行的是动态的sql语句 代码例如以下: BEGIN DECLARE in_data TEXT; /** 标记是否出错 */ DECLARE ...
- 南邮JAVA程序设计实验1 综合图形界面程序设计
南邮JAVA程序设计实验1 综合图形界面程序设计 实验目的: 学习和理解JAVA SWING中的容器,部件,布局管理器和部件事件处理方法.通过编写和调试程序,掌握JAVA图形界面程序设计的基本方法. ...
- windows下PTAM的编译
前些日子在研究PTAM,以下首先说说PTAM的编译过程,我在XP几WIN7搭配vs2010中均已測试过,都能够执行. 首先下载编译PTAM所必须的库文件.下载地址我会给出 PTAM(PTAM.zip) ...
- BigInteger类型转换成Long类型或int类型问题
BigInteger bi = new BigInteger("123"); int i = bi.intValue(); lo ...
- Vmware 安装samba
samba是什么samba是什么?能干什么? samba 是基于SMB协议(ServerMessage Block,信息服务块)的开源软件,samba也可以是SMB协议的商标.SMB是一种Linux. ...