UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
来源:互联网 发布:苹果数据线线序 编辑:程序博客网 时间:2024/06/05 14:25
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
解题报告
该题的题意是,给定一个有向图,对于每一个点v可以进行一个操作,把以v为终点的边的边权减少d,把以v为起点的边的边权增加d。最后要求所有的边权非负且最大。
所谓“非负且最大”就要求这个有向图中的所有边权都大于一个最大的数,这个数我们可以通过二分答案来找。
我们令作用于点u上的所有操作d的和为sum[u],那么对于连接了u、v(u->v)两个点的边I,他的边权在进行了所有操作后就会是:ed[i].w+sum[u]-sum[v]。也就是说ed[i].w+sum[u]-sum[v]>=x,整理得sum[v]-sum[u]>=ed[I].w-x,很显然可以用最长路的的方法来做。
#include<cstdio>#include<cstring>#include<queue>using namespace std;const int N=500;const int M=2700;const int INF=1e9;int head[N+5],num,dis[N+5],flag[N+5],cnt[N+5],n,m;struct edge{ int v,w,next;}e[M+5];void add(int u,int v,int w){ e[++num].v=v; e[num].w=w; e[num].next=head[u]; head[u]=num;}bool spfa(){ memset(flag,0,sizeof(flag)); memset(cnt,0,sizeof(cnt)); queue<int>q; for(int i=0;i<=n;i++){dis[i]=0;q.push(i);} while(!q.empty()) { int u=q.front(); q.pop(); flag[u]=false; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v; if(dis[v]>dis[u]+e[i].w)//最长路 { dis[v]=dis[u]+e[i].w; if(!flag[v]) { if(++cnt[v]>n)return false; flag[v]=true; q.push(v); } } } } return true;}bool change(int x){ for(int i=1;i<=n;i++) for(int j=head[i];j!=-1;j=e[j].next) e[j].w-=x; bool temp=spfa(); for(int i=1;i<=n;i++) for(int j=head[i];j!=-1;j=e[j].next) e[j].w+=x; return temp;}int main(){ while(~scanf("%d%d",&n,&m)) { memset(head,-1,sizeof(head)); num=0; int lf=1,rg=0; for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); rg=max(rg,w); } if(change(rg)) { printf("Infinite\n"); continue; } else if(!change(lf)) { printf("No Solution\n"); continue; } int ans=lf++;//这里是为了解决二分答案那个while循环进不去的输出问题 while(lf<rg) { int mid=(lf+rg)>>1; if(!change(mid))rg=mid; else { lf=mid+1; ans=mid; } } printf("%d\n",ans); } return 0;}
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- UVA11478 [Halum] 二分答案+SPFA差分约束系统
- uva11478 - Halum 二分+差分约束
- uva11478 Halum【二分+差分约束】
- UVA11478 Halum (差分约束)
- UVa11478 - Halum(差分约束)
- UVA 11478V Halum 二分答案+差分约束系统
- UVA11478-Halum(BellmanFord + 差分约束)
- uva 11478 Halum (差分约束+二分+SPFA判负环)
- UVA 11478 Halum 二分+差分约束+SPFA
- uva11478(差分约束,spfa求负环)
- Uva 11478 Halum(差分约束系统 + 二分)
- uva11478差分约束
- UVA - 11478 Halum(差分约束系统)
- POJ3169 差分约束系统 解题报告
- 差分约束POJ3159Candies(spfa+stack)解题报告
- UVA 11478 Halum(差分约束 SPFA判负环)
- UVA 11478 Halum(二分 + 差分约束)
- 【DL笔记】VGG网络详解及代码实现
- spark机器学习库之决策树分类
- vue返回上一页
- Hypervisor介绍
- Goroutine与Thread差异
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- SVM学习笔记-软间隔SVM
- MongoDB与Redis的比较
- LeetCode
- Java8源码-详解fail-fast
- 基于数组的栈实现
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- cookie之增删改查
- Spring MVC的使用