• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Warm up 16 Maze movement

Maze movement
Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB
Total submit users: 22, Accepted users: 22
Problem 12785 : No special judgement
Problem description
谁能告诉我这么写init(),n这个全局变量为什么会自动变为0啊,真的是感人肺腑!
Your boss gave you the task of creating a walking maze, and you are evaluating different designs. Before you commit to one, you want to know how quickly people can move in and out of each different maze. After all, your boss is interested in making money on this venture and, the faster people can move through, the more paying customers you can handle. A maze is a set of numbered rooms and passages connecting the rooms. The maze’s only entrance is at the lowest-numbered room and the only exit is at the highest-numbered room. Each passage has a limit in the number of people that can pass through at a time. For two rooms numbered x and y, if x and y have a common factor greater than one, then there is a passage between x and y. The largest common factor p is the number of people per minute that can walk from x to y. Simultaneously, p people per minute can also be walking from y to x. The entrance, exit, and rooms can handle any number of people walking through at a time. People want to get through the maze as quickly as possible, so they do not wait around in the rooms. Here are illustrations of the two sample inputs. Boxes represent the numbered rooms, and each arrow is a passage labeled by the number of people per minute that can walk through it.
Input
Input is a single maze description. The first line is an integer 2 ≤ n ≤ 1000 indicating the number of rooms in the maze. This is followed by n unique integers, one per line, which are the room numbers for the maze. Each room number is in the range [2, 2 × 10^9].
Output
Print the maximum number of people per minute that can enter the maze, assuming that people are exiting the maze at the same speed as people entering. No maze supports more than 10^9 people entering per minute.
Sample Input
Sample Input 1
4
4
6
8
9

Sample Input 2
7
25289
17017
2601
325
225
55223
190969
Sample Output
Sample Output 1
3

Sample Output 2
18

Problem Source
HNU Contest 
Submit   Discuss   Judge Status  Problems  Ranklist 
  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <map>
  3 #include <queue>
  4 #include <vector>
  5 #include <string>
  6 #include <cmath>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <cstdlib>
 10 #include <iostream>
 11 #include <algorithm>
 12 using namespace std;
 13 #define maxn 1035
 14 #define ll long long
 15 #define mod 1000000007
 16 #define INF 0x7fffffff
 17 #define eps 1e-8
 18 int n, m,s,t;
 19 struct Edge{
 20     int from, to,cap;
 21     ll flow;
 22     Edge(int a, int b, ll c, int d){ from = a; to = b; flow = c; cap = d; }
 23 };
 24 vector<Edge>edges;
 25 vector<int>g[maxn];
 26 bool vis[maxn];
 27 ll d[maxn];
 28 int a[maxn];
 29 int cur[maxn];
 30 int gcd(int n, int m){ return m ? gcd(m, n%m) : n; }
 31 void init(int n){
 32     for (int i = 0; i <= n; i++)g[i].clear();
 33     edges.clear();
 34 }
 35 void Add(int from, int to, ll flow, int cap){
 36     edges.push_back(Edge(from, to, flow, cap));
 37     edges.push_back(Edge(to, from, -flow, 0));
 38     int sz = edges.size();
 39     g[from].push_back(sz - 2);
 40     g[to].push_back(sz - 1);
 41 }
 42 ll dfs(int u,ll a){
 43     if (u == t || a == 0)return a;
 44     ll flow = 0, f;
 45     for (int& i = cur[u]; i < g[u].size(); i++){
 46         Edge& e = edges[g[u][i]];
 47         if (d[e.to] == d[u] + 1){
 48             f = dfs(e.to, min(a, e.cap - e.flow));
 49             if (f>0){
 50                 e.flow += f;
 51                 edges[g[u][i]^1].flow -= f;
 52                 flow += f;
 53                 a -= f;
 54                 if (a == 0)break;
 55             }
 56         }
 57     }
 58     return flow;
 59 }
 60 bool bfs(){
 61     memset(vis, 0, sizeof vis);
 62     queue<int>q;
 63     q.push(s); d[s] = 0; vis[s] = 1;
 64     while (!q.empty()){
 65         int u = q.front(); q.pop();
 66         for (int i = 0; i < g[u].size(); i++){
 67             Edge& e = edges[g[u][i]];
 68             if (!vis[e.to] && e.cap>e.flow){
 69                 vis[e.to] = 1;
 70                 d[e.to] = d[u] + 1;
 71                 q.push(e.to);
 72             }
 73         }
 74     }
 75     return vis[t];
 76 }
 77 ll dinic(int s,int t){
 78     ll flow = 0;
 79     while (bfs()){
 80         memset(cur, 0, sizeof cur);
 81         flow += dfs(s, INF);
 82     }
 83     return flow;
 84 }
 85 int main(){
 86     /*int t;
 87     scanf("%d", &t);
 88     while (t--){
 89     scanf("%I64d", &n);
 90 
 91     }*/
 92     //ll x, y, x1, x2, y1, y2, s1, s2;
 93     int n;
 94     while (~scanf("%d", &n)){
 95         s = INF; t = -INF;
 96         for (int i = 0; i < n; i++){
 97             scanf("%d", &a[i]);
 98             s = min(s, a[i]);
 99             t = max(t, a[i]);
100         }
101         for (int i = 0; i < n; i++)if (a[i] == s){s = i; break;}
102         for (int i = 0; i < n; i++)if (a[i] == t){t = i; break; }
103         for (int i = 0; i < n; i++)
104             for (int j = 0; j < n; j++){
105                 m = gcd(a[i], a[j]);
106                 if (m != 1&&i!=j)Add(i, j, 0,m);
107             }
108         ll ans=dinic(s,t);
109         printf("%I64d\n", ans);
110         init(n);
111     }
112     return 0;
113 }
View Code
posted @ 2013-11-04 02:49  HaibaraAi  阅读(134)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3