UVA 11478 Halum
2015-08-04 22:29
369 查看
Halum
Time Limit: 3000msMemory Limit: 131072KB
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 = 510; struct arc { int to,w,next; arc(int x = 0,int y = 0,int z = -1) { to = x; w = y; next = z; } } e[500000]; 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 = 1; i <= n; ++i) { cnt[i] = 1; d[i] = 0; 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,-1,sizeof head); int low = 1,high = 0; for(int i = tot = 0; i < m; ++i) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); high = max(high,w); } if(!spfa(1)) puts("No Solution"); else if(spfa(high+1)) puts("Infinite"); else { int ret; while(low <= high) { int mid = (low + high)>>1; if(spfa(mid)) { ret = mid; low = mid+1; } else high = mid - 1; } printf("%d\n",ret); } } return 0; }
View Code
相关文章推荐
- First Missing Positive
- Docker容器的跨主机访问
- Git 版本控制使用
- POJ 1160:Post Office 邮局经典DP
- PCIe 配置空间读写内核实现
- 理解python的with语句
- POJ 1160:Post Office 邮局经典DP
- Java之File类及递归树形展示目录_20150804
- getcwd()和dirname(__FILE__)的区别
- 智慧城市
- Docker源码分析(六):DOCKER DAEMON网络
- kthread_run函数
- Linux伙伴系统
- 二叉树
- [leedcode 208] Implement Trie (Prefix Tree)
- oracle nls_lang 设置
- android studio "Cannot launch SDK manager ...."
- scp报错:not a regular file
- RCP开发之comand(命令)
- Linux内存管理--伙伴系统和内存分配器