题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大。若无解,输出结果。若可无限大,输出结果。否则,输出最小边权的最大值。

思路:差分约束系统用最短路来解。列式子后建图,新图的边就是原图的边,权值也不变。有3种情况,要分开判断。

  (1)若连最小的权值1都达不到,肯定无解。

  (2)若可以超过所给边的最大权值,那么最小权值肯定可以继续增大。

  (3)接下来用二分猜答案,答案范围在[1,big]。只要无负环,就是可取的。

  注意:很容易超时,优化一下spfa吧。

700ms+

#include <bits/stdc++.h>
#define INF 0x7f7f7f7f
#define pii pair<int,int>
#define LL unsigned long long
using namespace std;
const int N=;
struct node
{
int from,to,cost;
node(){};
node(int from,int to,LL cost):from(from),to(to),cost(cost){};
}edge[];
vector<int > vect[N];
int edge_cnt; void add_node(int from,int to,int cost)
{
edge[edge_cnt]=node(from,to,cost);
vect[from].push_back(edge_cnt++);
} int inq[N], cost[N], cnt[N];
bool spfa(int s, int n, int d)
{
memset(cost, , sizeof(cost));
memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt)); deque<int> que(,s);
inq[s]=;
while(!que.empty())
{
int x=que.front();que.pop_front();
inq[x]=;
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(cost[e.to]>cost[x]+e.cost-d )
{
cost[e.to]=cost[x]+e.cost-d;
if(!inq[e.to])
{
inq[e.to]=;
if(++cnt[e.to]>n) return false;
//优化很有用
if(!que.empty()&&cost[e.to]<cost[que.front()]) que.push_front(e.to);
else que.push_back(e.to);
}
}
}
}
return true;
} int cal(int s,int n, int L, int R)
{
while(L<R)
{
LL mid=L+(R-L+)/;
if( spfa(s, n, mid) ) L=mid; //不产生环,则满足要求
else R=mid-; //大过头了也可能产生环的
}
return L;
} int main()
{
freopen("input.txt", "r", stdin);
int n, m, a, b, c;
while(~scanf("%d %d",&n,&m))
{
edge_cnt=;
memset(edge,,sizeof(edge));
for(int i=; i<=n; i++) vect[i].clear();
int big=; for(int i=; i<m; i++)
{
scanf("%d %d %d", &a, &b, &c);
add_node(a, b, c);
big=max(big, c);
}
for(int i=; i<=n; i++) add_node(, i, ); //加源点
if( spfa(, n, big+) ) //如果连最大权都能超过,肯定可以inf。因为若有环,一般都是有权加就有权减。
{
puts("Infinite");
continue;
}
if( !spfa(, n, ) ) //没有大于0的边
{
puts("No Solution");
continue;
}
printf("%d\n", cal(, n, , big) );
} return ;
}

AC代码

UVA 11374 Halum (差分约束系统,最短路)的更多相关文章

  1. UVA11478 Halum [差分约束系统]

    https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...

  2. [BZOJ2330][SCOI2011]糖果 差分约束系统+最短路

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 类似于题目中这种含有不等式关系,我们可以建立差分约束系统来跑最长路或最短路. 对于一 ...

  3. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  4. UVA 11478 Halum (差分约束)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  5. UVA 11374 Airport Express (最短路)

    题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...

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

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

  7. UVA - 11478 Halum 二分+差分约束

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 题意: 给定一个有向图,每一条边都有一个权值,每次你可以 ...

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

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

  9. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

随机推荐

  1. adb或appium下多设备中指定设备的启动

    一.先用adb devices列出所有设备序列号 List of devices attached012BDC7N78954789 device132AEC8N57897458 device 二.进入 ...

  2. C内联汇编

    用C写程序比直接用汇编写程序更简洁,可读性更好,但效率可能不如汇编程序,因为C程序毕竟要经由编译器生成汇编代码,尽管现代编译器的优化已经做得很好了,但还是不如手写的汇编代码.另外,有些平台相关的指令必 ...

  3. 安装WINCC6.0的步骤

    安装WINCC6.0/6.2的步骤 (XP不能是HOME版的!!!) 1.    首先安装SQL FOR WINCC6.0/6.2这个软件(如果你的系统已安装此软件相关版本可能提示安装失败请卸载后再重 ...

  4. SharePoint Server 2007 简体中文下载

    SharePoint Server 2007 简体中文下载 2010-12-16 10:56 正式版key SN: Tkjcb-3wkhk-2ty2t-qymk2-9xm2y 这个版本也是通过Key来 ...

  5. QTP10补丁汇总

    QTP10补丁汇总 QTP_00591.EXE QTP10 调试器视图问题的补丁 QTP_00591 - Prevent QuickTest Debug Viewer Problems when Pr ...

  6. 添加dubbo xsd的支持

    使用dubbo时遇到问题: org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'htt ...

  7. Tomcat常见内存溢出的解决办法

    PermGen space错误解决方法 在看下文之前,首先要确认意见事情,就是你是如何启动tomcat的,我们在平时的开发环境当中,都是通过startup.bat方式启动tomcat的,那么你按照下面 ...

  8. 312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  9. CentOS增加硬盘

    1.查看新硬盘     #fdisk –l      新添加的硬盘的编号为/dev/sdb 2.硬盘分区     1)进入fdisk模式     #/sbin/fdisk /dev/sdb     2 ...

  10. 转:C# 通过委托更新UI(异步加载)

    来自:http://blog.csdn.net/gongzhe2011/article/details/27351853 using System.Windows.Forms; using Syste ...