SGU 438 -- The Glorious Karlutka River =)
2015-10-16 07:16
309 查看
代码实现:
#include<cstdio> #include<cstring> #include<queue> #include<iostream> #include<vector> #include<cmath> #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define P(a, b) ((a-1)*50+(b)) #define Mac(a) ((a)+5000) using namespace std; const int N=10005, M=1210010, INF=0x3f3f3f3f; int n, m, d, w, s, t, top; int head , gap , cur , dis , vis , pre , g[55][55]; struct Point{ int x, y, cap; }p[55]; struct Edge{ int to, next, flow; Edge(int _to = 0, int _next = 0, int _flow = 0):to(_to), next(_next), flow(_flow){} }edge[M]; bool Judge(int a, int b){ double x = d, y = sqrt(double((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y))); if(x - y >= -1e-6) return 1; return 0; } void Addedge(int from, int to, int flow){ edge[top] = Edge(to, head[from], flow); head[from] = top++; edge[top] = Edge(from, head[to], 0); head[to] = top++; } void Bfs(){ queue<int> q; memset(gap, 0, sizeof(gap)); memset(dis, -1, sizeof(dis)); gap[0] = 1; dis[t] = 0; q.push(t); while(!q.empty()){ int u = q.front(); q.pop(); for(int i = head[u]; i+1; i = edge[i].next){ if(dis[edge[i].to] == -1){ dis[edge[i].to] = dis[u] + 1; gap[dis[edge[i].to]] ++; q.push(edge[i].to); } } } } int Sap(){ Bfs(); memset(pre, -1, sizeof(pre)); for(int i = s; i <= t; ++i) cur[i] = head[i]; int u = s, cur_flow, max_flow = 0, neck, tmp; while(dis[s] <= t){ if(u == t){ cur_flow = INF; for(int i = s; i != t; i = edge[cur[i]].to){ if(cur_flow > edge[cur[i]].flow){ cur_flow = edge[cur[i]].flow; neck = i; } } for(int i = s; i != t; i = edge[cur[i]].to){ tmp = cur[i]; edge[tmp].flow -= cur_flow; edge[tmp^1].flow += cur_flow; } max_flow += cur_flow; u = neck; } int i; for(i = cur[u]; i + 1; i = edge[i].next){ if(edge[i].flow && dis[u] == dis[edge[i].to] + 1) break; } if(i != -1){ cur[u] = i; pre[edge[i].to] = u; u = edge[i].to; }else{ if(--gap[dis[u]] == 0) break; cur[u] = head[u]; int mindis = t; for(i = head[u]; i + 1; i = edge[i].next) if(edge[i].flow && mindis > dis[edge[i].to]) mindis = dis[edge[i].to]; dis[u] = mindis + 1; gap[dis[u]] ++; if(u != s) u = pre[u]; } } return max_flow; } int main(){ while(~scanf("%d%d%d%d", &n, &m, &d, &w)){ memset(head, -1, sizeof(head)); memset(g, 0, sizeof(g)); top = s = 0; t = 10001; int ans = 0, res, flag = 0; for(int i = 1; i <= n; ++i) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].cap); if(d >= w){ printf("1\n"); continue; } for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) if(i != j && Judge(i, j)) g[i][j] = 1; for(int i = 1; i <= n+m; ++i){ for(int j = 1; j <= n; ++j){ if(p[j].y <= d) Addedge(s, P(i, j), INF); if(p[j].y+d >= w) Addedge(Mac(P(i, j)), t, INF); Addedge(P(i, j), Mac(P(i, j)), p[j].cap); for(int k = 1; k <= n; ++k){ if(k != j && g[j][k] == 1) Addedge(Mac(P(i, j)), P(i+1, k), INF); } } res = Sap(); ans += res; if(ans >= m){ flag = 1; printf("%d\n", i + 1); break; } } if(!flag) printf("IMPOSSIBLE\n"); } }
相关文章推荐
- SGU 326 -- Perspective
- STM32入门学习之USART(STM32F030F4P6基于CooCox IDE)
- ZOJ 2760 -- How Many Shortest Path
- 史上最全的iOS面试题及答案
- POJ 3281 -- Dining
- Java实现约瑟夫环
- POJ 2391 -- Ombrophobic Bovines
- iOS 9新系统下App Store应用上传新指南
- POJ 1637 -- Sightseeing tour
- POJ 1149--PIGS
- mysql 学习记录(十二)--触发器
- Amazon OA2准备——Round Robin Java实现
- 神奇的卷积神经网络(三)
- quartz表达式
- 记录一次linux服务器环境的配置
- 扫描和嗅探
- reading words in your computer and changing to female voice, linux festival text2wave saving wav files
- (IOS)BaiduFM 程序分析
- ValuesQuerySet对象 转换成json
- 复制ue中的十六进制内容