UVA 11478 查分约束+二分答案 解题报告
2017-07-10 20:17
411 查看
让我看到你们的双手
Halum
Description
You are given a directed graph G(V; E) with a set of vertices and edges. Each edge (i; j) that connectssome vertex i to vertex j has an integer cost associated with that edge.
Dene the operation Halum(v; d) to operate on a vertex v using an integer d as follows: subtractd 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 twoedges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2; 3) operates onedges 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 linerepresents 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
【解题报告】
卡了好久,身心疲惫,烦躁异常,什么也不想说。
代码如下:
Halum
Description
You are given a directed graph G(V; E) with a set of vertices and edges. Each edge (i; j) that connectssome vertex i to vertex j has an integer cost associated with that edge.
Dene the operation Halum(v; d) to operate on a vertex v using an integer d as follows: subtractd 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 twoedges. Edge (1, 2) has cost -1, and edge (2,3) has cost 1. The operation Halum(2; 3) operates onedges 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 linerepresents 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<cstring> #include<algorithm> #include<queue> using namespace std; #define N 27005 #define inf 0x3f3f3f3f int n,m,num; int head ,dis ,vis ,cnt ; struct Edge { int to,nxt,w; }e ; void adde(int u,int v,int w) { e[num].w=w; e[num].to=v; e[num].nxt=head[u]; head[u]=num++; } bool SPFA() { memset(vis,0,sizeof(vis)); memset(dis,inf,sizeof(dis)); memset(cnt,0,sizeof(cnt)); queue<int> q; q.push(0); vis[0]=cnt[0]=1;dis[0]=0; while(!q.empty()) { int now=q.front();q.pop(); vis[now]=0; for(int i=head[now];i!=-1;i=e[i].nxt) { int id=e[i].to; if(dis[id]>dis[now]+e[i].w) { dis[id]=dis[now]+e[i].w; if(!vis[id]) { if(++cnt[id]>n) return false; vis[id]=1; q.push(id); } } } } return true; } bool check(int x) { for(int i=1;i<=n;++i) for(int j=head[i];~j;j=e[j].nxt) e[j].w-=x; bool flag=SPFA(); for(int i=1;i<=n;++i) for(int j=head[i];~j;j=e[j].nxt) e[j].w+=x; return flag; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); num=0; for(int i=1;i<=n;++i) adde(0,i,0); int L=1,R=0; for(int i=1;i<=m;++i) { int from,to,val; scanf("%d%d%d",&from,&to,&val); adde(from,to,val); if(val>R) R=val; } if(check(R)) {puts("Infinite");continue;} else if(!check(L)) {puts("No Solution");continue;} int ans=L++; while(L<R) { int mid=L+((R-L)>>1); if(!check(mid)) R=mid; else { L=mid+1; ans=mid; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- UVA11478 Halum 解题报告【图论】【二分答案】【SPFA】【差分约束系统】
- [UVA]11478 二分答案+差分约束
- UVA11090 Going in Cycle!! 解题报告【判负环】【SPFA】【二分答案】
- UVAlive 3890&Poj3525 半平面交+二分 解题报告
- UVa 1422 Processor 解题报告(二分)
- LuoguP2680/UOJ150[NOIP2015] 运输计划 解题报告【二分答案+树上操作(LCA)+树上差分】
- Uva 12124 Assemble 解题报告(二分)
- UVA 11478(差分约束 + 二分)
- BZOJ 1052 二分答案 解题报告
- SDUT 2778 小明的花费预算 (二分答案) -- 解题报告
- 17.8.19 校内赛 解题报告【求线段交点+凸包+求多边形面积】【判定点是否在多边形内】【二分答案+半平面交】
- LuoguP1083 借教室[NOIP2012] 解题报告【二分答案+差分】
- LuoguP1314[NOIP2011] 聪明的质监员 解题报告【二分答案+前缀和】
- SPOJ 287 网络流最大流+二分答案 解题报告
- 17.7.24 校内赛 解题报告【二分答案】【记忆化搜索】【数据结构】
- Uva 1350 - Pinary 解题报告(斐波那契+二分)
- BZOJ 1822 计算几何+网络流+二分答案 解题报告
- UVA 11478 Halum(二分 + 差分约束)
- Uva 1335 Beijing Guards 解题报告(策略+二分)
- UVa 1450 Airport 解题报告(二分+策略)