USACO Contest代码库

USACO Contest 2001

  • Spring
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <algorithm>
      4 
      5 using namespace std;
      6 
      7 const int MAXN=85;
      8 const int SIZE=730;
      9 const int delta=360;
     10 
     11 int low[MAXN][MAXN], upp[MAXN][MAXN];
     12 bool hash[MAXN][MAXN][SIZE];
     13 bool np[MAXN][MAXN];
     14 char expr[MAXN];
     15 int n;
     16 
     17 int main()
     18 {
     19     freopen("cowfix.in", "r", stdin);
     20     freopen("cowfix.out", "w", stdout);
     21     scanf("%s", expr+1);
     22     n=strlen(expr+1);
     23     memset(hash, 0, sizeof(hash));
     24     memset(low, 0, sizeof(low));
     25     memset(upp, 0, sizeof(upp));
     26     memset(np, 0, sizeof(np));
     27 
     28     for (int i=1; i<=n; i++)
     29         if (expr[i]>='0'&&expr[i]<='9')
     30         {
     31             np[i][i]=hash[i][i][delta+expr[i]-'0']=true;
     32             low[i][i]=upp[i][i]=expr[i]-'0';
     33         }
     34     for (int k=3; k<=n; k++)
     35         for (int i=1; i<=n-k+1; i++)
     36             if (k&1)
     37             {
     38                 int opt=0, right=i+k-1, ret;;
     39                 for (int j=i; j<=right; j++)
     40                     if (expr[j]=='-'||expr[j]=='+') opt++;
     41                 if (k-opt==opt+1)
     42                 {
     43                     for (int j=i; j<=right; j++)
     44                         if (expr[j]=='-'||expr[j]=='+')
     45                         {
     46                             //Infix
     47                             if (np[i][j-1]&&np[j+1][right])
     48                             {
     49                                 np[i][right]=true;
     50                                 for (int p1=low[i][j-1]; p1<=upp[i][j-1]; p1++)
     51                                 {
     52                                     if (!hash[i][j-1][p1+delta]) continue;
     53                                     for (int p2=low[j+1][right]; p2<=upp[j+1][right]; p2++)
     54                                     {
     55                                         if (!hash[j+1][right][p2+delta]) continue;
     56                                         if (expr[j]=='+') ret=p1+p2;
     57                                         else ret=p1-p2;
     58                                         hash[i][right][ret+delta]=true;
     59                                         low[i][right]=min(low[i][right], ret);
     60                                         upp[i][right]=max(upp[i][right], ret);
     61                                     }
     62                                 }
     63                             }
     64                             //Postfix
     65                             if (j==right)
     66                             {
     67                                 for (int m=i; m<j; m++)
     68                                 {
     69                                     if (!(np[i][m]&&np[m+1][j-1])) continue;
     70                                     np[i][right]=true;
     71                                     for (int p1=low[i][m]; p1<=upp[i][m]; p1++)
     72                                     {
     73                                         if (!hash[i][m][p1+delta]) continue;
     74                                         for (int p2=low[m+1][j-1]; p2<=upp[m+1][j-1]; p2++)
     75                                         {
     76                                             if (!hash[m+1][j-1][p2+delta]) continue;
     77                                             if (expr[j]=='+') ret=p1+p2;
     78                                             else ret=p1-p2;
     79                                             hash[i][right][ret+delta]=true;
     80                                             low[i][right]=min(low[i][right], ret);
     81                                             upp[i][right]=max(upp[i][right], ret);
     82                                         }
     83                                     }
     84                                 }
     85                             }
     86                             //Prefix
     87                             if (j==i)
     88                             {
     89                                 for (int m=j+1; m<=right; m++)
     90                                 {
     91                                     if (!(np[j+1][m]&&np[m+1][right])) continue;
     92                                     np[i][right]=true;
     93                                     for (int p1=low[j+1][m]; p1<=upp[j+1][m]; p1++)
     94                                     {
     95                                         if (!hash[j+1][m][p1+delta]) continue;
     96                                         for (int p2=low[m+1][right]; p2<=upp[m+1][right]; p2++)
     97                                         {
     98                                             if (!hash[m+1][right][p2+delta]) continue;
     99                                             if (expr[j]=='+') ret=p1+p2;
    100                                             else ret=p1-p2;
    101                                             hash[i][right][ret+delta]=true;
    102                                             low[i][right]=min(low[i][right], ret);
    103                                             upp[i][right]=max(upp[i][right], ret);
    104                                         }
    105                                     }
    106                                 }
    107                             }
    108                         }
    109                 }
    110             }
    111     int ans=0;
    112     for (int i=low[1][n]; i<=upp[1][n]; i++)
    113         if (hash[1][n][i+delta]) ans++;
    114     printf("%d\n", ans);
    115     fclose(stdin); fclose(stdout);
    116     return 0;
    117 }
    PROBLEM 1: Cowfix [Brian Dean, 2001]
     1 #include <cstdio>
     2 #include <cstring>
     3 
     4 const int MAXN=1005;
     5 const int inf=1000000000;
     6 
     7 struct node
     8 {
     9     int r, c, s;
    10 };
    11 
    12 node cow[MAXN];
    13 int n, a, b;
    14 
    15 int main()
    16 {
    17     freopen("spots.in", "r", stdin);
    18     freopen("spots.out", "w", stdout);
    19     scanf("%d%d%d", &n, &a, &b);
    20     for (int i=0; i<n; i++)
    21         scanf("%d%d%d", &cow[i].r, &cow[i].c, &cow[i].s);
    22     int ret=0;
    23     for (int i=0; i<n; i++)
    24     {
    25         int min=inf, max=0;
    26         for (int j=0; j<n; j++)
    27         {
    28             if (!(cow[j].r>=cow[i].r&&cow[j].c>=cow[i].c)) continue;
    29             if (!(cow[j].r<cow[i].r+a&&cow[j].c<cow[i].c+b)) continue;
    30             if (cow[j].s>max) max=cow[j].s;
    31             if (cow[j].s<min) min=cow[j].s;
    32         }
    33         if (ret<max-min) ret=max-min;
    34         min=inf; max=0;
    35         for (int j=0; j<n; j++)
    36         {
    37             if (!(cow[j].r>cow[i].r-a&&cow[j].c>=cow[i].c)) continue;
    38             if (!(cow[j].r<=cow[i].r&&cow[j].c<cow[i].c+b)) continue;
    39             if (cow[j].s>max) max=cow[j].s;
    40             if (cow[j].s<min) min=cow[j].s;
    41         }
    42         if (ret<max-min) ret=max-min;
    43     }
    44     printf("%d\n", ret);
    45     fclose(stdin); fclose(stdout);
    46     return 0;
    47 }
    PROBLEM 2: Cattle Spotting [Brian Dean, 2001]
      1 #include <cmath>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 
      6 const int MAXN=105;
      7 const int MAXF=205;
      8 const int MAXK=505;
      9 
     10 typedef struct point{int x, y;}point;
     11 typedef struct fence{point a, b; int h;}fence;
     12 
     13 double dis[MAXK][MAXN];
     14 double map[MAXN][MAXN];
     15 point cow[MAXN];
     16 fence fen[MAXF];
     17 point barn;
     18 int n, f, k;
     19 
     20 void init()
     21 {
     22     scanf("%d%d%d%d%d", &n, &k, &f, &cow[0].x, &cow[0].y);
     23     for (int i=1; i<=n; i++)
     24         scanf("%d%d", &cow[i].x, &cow[i].y);
     25     for (int i=0; i<f; i++)
     26         scanf("%d%d%d%d%d", &fen[i].a.x, &fen[i].a.y, &fen[i].b.x, &fen[i].b.y, &fen[i].h);
     27 }
     28 
     29 int cross(point a, point b, point c)
     30 {
     31     double tmp=(double)(b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
     32     if (tmp>0) return 1;
     33     if (tmp==0) return 0;
     34     else return -1;
     35 }
     36 
     37 bool check(point a, point b, point c, point d)
     38 {
     39     int tmp1=cross(a, b, d)*cross(a, b, c);
     40     int tmp2=cross(c, d, a)*cross(c, d, b);
     41     if (tmp1<0&&tmp2<0) return true;
     42     else return false;
     43 }
     44 
     45 void build()
     46 {
     47     for (int i=0; i<=n; i++)
     48     {
     49         for (int j=i+1; j<=n; j++)
     50         {
     51             map[i][j]=1;
     52             for (int l=0; l<f; l++)
     53                 if (check(cow[i], cow[j], fen[l].a, fen[l].b))
     54                     map[i][j]*=(double)1/fen[l].h;
     55             map[j][i]=map[i][j];
     56         }
     57         map[i][i]=1;
     58     }
     59     for (int l=0; l<=n; l++)
     60         for (int i=0; i<=n; i++)
     61             for (int j=0; j<=n; j++)
     62                 if (map[i][j]<map[i][l]*map[l][j])
     63                     map[i][j]=map[i][l]*map[l][j];
     64 }
     65 
     66 void print(double x)
     67 {
     68     int b=0;
     69     while (x<1)
     70     {
     71         b++;
     72         x*=10;
     73     }
     74     if (b==0) printf("1.0000e00");
     75     else if (b<10) printf("%.4fe-0%d\n", x, b);
     76     else printf("%.4fe-%d\n", x, b);
     77 }
     78 
     79 void solve()
     80 {
     81     for (int i=1; i<=n; i++)
     82         dis[1][i]=map[0][i];
     83     for (int l=2; l<=k; l++)
     84         for (int i=1; i<=n; i++)
     85             for (int j=1; j<=n; j++)
     86                 if (i!=j&&dis[l][i]<dis[l-1][j]*map[j][i])
     87                     dis[l][i]=dis[l-1][j]*map[j][i];
     88     double ans=0;
     89     for (int i=1; i<=n; i++)
     90         for (int j=1; j<=k; j++)
     91             if (ans<dis[j][i]*dis[k+1-j][i])
     92                 ans=dis[j][i]*dis[k+1-j][i];
     93     print(ans);
     94 }
     95 
     96 int main()
     97 {
     98     freopen("route.in", "r", stdin);
     99     freopen("route.out", "w", stdout);
    100     init();
    101     build();
    102     solve();
    103     fclose(stdin); fclose(stdout);
    104     return 0;
    105 }
    PROBLEM 3: The Milk Route [Brian Dean, 2001]
      1 #include <queue>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 
      6 using namespace std;
      7 
      8 const int MAXN=1000;
      9 const int MAXM=50005;
     10 const int inf=100000000;
     11 
     12 struct node
     13 {
     14     int v, cap, flow;
     15     node *ne, *op;
     16 };
     17 
     18 node *head[MAXN], *work[MAXN];
     19 int lev[MAXN];
     20 int n, m;
     21 int S, T;
     22 
     23 void addedge(int u, int v, int cap)
     24 {
     25     node *pu=new(node), *pv=new(node);
     26     pu->v=v; pu->cap=cap; pu->flow=0; pu->ne=head[u];
     27     pv->v=u; pv->cap=0; pv->flow=0; pv->ne=head[v];
     28     head[u]=pu; head[u]->op=pv;
     29     head[v]=pv; head[v]->op=pu;
     30 }
     31 
     32 bool dinic_bfs()
     33 {
     34     queue<int>q;
     35     while (!q.empty()) q.pop();
     36     memset(lev, 0, sizeof(lev));
     37     lev[S]=1; q.push(S);
     38     while (!q.empty())
     39     {
     40         int u=q.front(); q.pop();
     41         for (node *p=head[u]; p; p=p->ne)
     42             if (!lev[p->v]&&p->cap>p->flow)
     43             {
     44                 lev[p->v]=lev[u]+1;
     45                 q.push(p->v);
     46             }
     47     }
     48     return (lev[T]>0);
     49 }
     50 
     51 int dinic_dfs(int u, int low)
     52 {
     53     if (u==T) return low;
     54     for (node *p=work[u]; p; p=p->ne)
     55     {
     56         int v=p->v; work[u]=p;
     57         if (lev[v]==lev[u]+1&&p->cap>p->flow)
     58         {
     59             int tmp=dinic_dfs(v, min(low, p->cap-p->flow));
     60             if (tmp)
     61             {
     62                 p->flow+=tmp;
     63                 p->op->flow-=tmp;
     64                 return tmp;
     65             }
     66         }
     67     }
     68     return 0;
     69 }
     70 
     71 int dinic()
     72 {
     73     int maxflow=0;
     74     while (dinic_bfs())
     75     {
     76         memcpy(work, head, sizeof(head));
     77         while (1)
     78         {
     79             int tmp=dinic_dfs(S, inf);
     80             if (!tmp) break;
     81             maxflow+=tmp;
     82         }
     83     }
     84     return maxflow;
     85 }
     86 
     87 int main()
     88 {
     89     freopen("barn.in", "r", stdin);
     90     freopen("barn.out", "w", stdout);
     91     scanf("%d%d", &n, &m);
     92     memset(head, 0, sizeof(head));
     93     for (int i=0; i<m; i++)
     94     {
     95         int u, v;
     96         scanf("%d%d", &u, &v);
     97         addedge(u+n, v, inf);
     98         addedge(v+n, u, inf);
     99     }
    100     for (int i=3; i<=n; i++)
    101         addedge(i, i+n, 1);
    102     addedge(1, 1+n, inf); addedge(2, 2+n, inf);
    103     S=1; T=n+2;
    104     printf("%d\n", dinic());
    105     fclose(stdin); fclose(stdout);
    106     return 0;
    107 }
    PROBLEM 4: Traveling Cows [Brian Dean, 1999]
  • Open
  • Fall
  • Winter

USACO Contest 2002

USACO Contest 2003

USACO Contest 2004

USACO Contest 2005

USACO Contest 2006

USACO Contest 2007

USACO Contest 2008

USACO Contest 2009

USACO Contest 2010

USACO Contest 2011

USACO Contest 2012

USACO Contest 2013

 

posted @ 2013-05-26 20:52  nevergo  阅读(312)  评论(0编辑  收藏  举报