Usaco 2006 November gold

Source: Internet
Author: User

Poj 3253 fence repair STL heap operation

I would like to say that stacking in STL is the most painful operation I have seen currently.

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <utility>#include <vector>#include <queue>#include <map>#include <set>#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)>(y)?(y):(x))using namespace std;int n,a[20005];int main(){    scanf("%d",&n);    for(int i=0; i<n; i++)        scanf("%d",&a[i]);    vector< int > v(a,a+n);    make_heap(v.begin(),v.end(),greater< int >());    long long ans=0;    for(int i=1; i<=n-1; i++)    {        long long tmp=v.front();        pop_heap(v.begin(),v.end(),greater< int >());        v.pop_back();        tmp+=v.front();        pop_heap(v.begin(), v.end(),greater< int >());        v.pop_back();        v.push_back(tmp);        push_heap(v.begin(),v.end(),greater< int >());        ans+=tmp;        printf("%d\n", tmp);    }    printf("%lld\n",ans);    return 0;}
View code

 

Poj 3254 corn fields state compression recurrence

UC [] indicates the case where the adjacent two are not at the same time 1, 377, so N * 377 ^ 2

Note that bit operation Priority is lower than =!

#include <cstdio>#include <cstring>#define MOD 100000000int n,m,a[20],k;int uc[500],all;int f[20][5000];int main(){    scanf("%d%d",&n,&m);    for(int i=0; i<n; i++)        for(int j=0; j<m; j++)        {            scanf("%d",&k);            a[i]=(a[i]<<1)+k;        }    for(int i=0; i<(1<<m); i++)    {        int tmi=i,flag=0;        while(tmi)        {            if((tmi&3)==3) flag=1;            tmi>>=1;        }        if(!flag)        {            uc[all++]=i;            if((i|a[0])==a[0])                f[0][i]=1;        }    }    for(int i=1; i<n; i++)        for(int j=0; j<all; j++)        if((a[i]|uc[j])==a[i])        {            for(int k=0; k<all; k++)            if(!(uc[j]&uc[k]))            {                f[i][uc[j]]=(f[i][uc[j]]+f[i-1][uc[k]])%MOD;            }        }    int ans=0;    for(int j=0; j<all; j++)        ans=(ans+f[n-1][uc[j]])%MOD;    printf("%d\n", ans);    return 0;}
View code

 

Poj 3255 A * Algorithm for K Short Circuit

I got to know about a * for the first time.

----------------- Wikipedia ----------------------------

If G (n) represents the actual distance from the starting point to any vertex N, and H (n) Represents the estimated distance from any vertex N to the target vertex, then the formula of the * algorithm is: F (n) = g (n) + H (n ). This formula follows the following features:

  • If H (n) is 0, you only need to find G (N), that is, find the shortest path from the starting point to any vertex N, then convert it into a single-source shortest path problem, that is, Dijkstra Algorithm
  • If H (n) <= "n to the actual distance from the target", the optimal solution can be obtained. In addition, the smaller the H (n), the more nodes to be calculated, and the lower the algorithm efficiency.

-------------------------------------------------

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cstdlib>#include <cmath>#include <utility>#include <vector>#include <queue>#include <map>#include <set>#define max(x,y) ((x)>(y)?(x):(y))#define min(x,y) ((x)>(y)?(y):(x))#define INF 0x3f3f3f3fusing namespace std;struct Edge{    int y,w,ne;}e[200005];int x,y,w,n,m;int be[5005],all;int h[5005];bool vis[5005];struct Point{    int x,g;    bool operator < (const Point T) const    {        return g+h[x]>T.g+h[T.x];    }};void add(int x, int y, int w){    e[all].y=y;    e[all].w=w;    e[all].ne=be[x];    be[x]=all++;}void SPFA(int s){    queue< int > q;    for(int i=0; i<=n; i++)    {        h[i]=INF;        vis[i]=0;    }    h[s]=0;    vis[s]=1;    while(!q.empty())        q.pop();    q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=0;        for(int i=be[u]; i!=-1; i=e[i].ne)        {            int v=e[i].y;            if(h[v]>h[u]+e[i].w)            {                h[v]=h[u]+e[i].w;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}int Astar(int s, int t, int k){    SPFA(t);    priority_queue< Point > q;    while(!q.empty())        q.pop();    memset(vis,0,sizeof(vis));    Point cur,nxt;    cur.x=s;    cur.g=0;    q.push(cur);    while(!q.empty())    {        cur=q.top();        q.pop();        if(cur.x==t && !--k)            return cur.g;        for(int i=be[cur.x]; i!=-1; i=e[i].ne)        {            nxt.x=e[i].y;            nxt.g=cur.g+e[i].w;            q.push(nxt);        }    }    return -1;}void init(){    all=0;    memset(be,-1,sizeof(be));}int main(){    scanf("%d%d",&n,&m);    init();    for(int i=0; i<m; i++)    {        scanf("%d%d%d",&x,&y,&w);        add(x,y,w);        add(y,x,w);    }    printf("%d\n",Astar(1,n,2));    return 0;}
View code

 

Usaco 2006 November gold

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.