zoukankan      html  css  js  c++  java
  • poj 1463 Strategic game DP

    题目地址:http://poj.org/problem?id=1463

    题目:

    Strategic game
    Time Limit: 2000MS   Memory Limit: 10000K
    Total Submissions: 7929   Accepted: 3692

    Description

    Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him? 

    Your program should find the minimum number of soldiers that Bob has to put for a given tree. 

    For example for the tree: 

    the solution is one soldier ( at the node 1).

    Input

    The input contains several data sets in text format. Each data set represents a tree with the following description: 

    • the number of nodes 
    • the description of each node in the following format 
      node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifiernumber_of_roads 
      or 
      node_identifier:(0) 

    The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500);the number_of_roads in each line of input will no more than 10. Every edge appears only once in the input data.

    Output

    The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following:

    Sample Input

    4
    0:(1) 1
    1:(2) 2 3
    2:(0)
    3:(0)
    5
    3:(3) 1 4 2
    1:(1) 0
    2:(0)
    0:(0)
    4:(0)

    Sample Output

    1
    2

    Source

     
    思路:明显树形dp嘛,然而写完一交,MLE的我不省人事,这题目居然这么变态卡内存,,
       我把记录边的vector改成链式前向星就ac了
       这个和上题思路一样,就不啰嗦了,可以去看看前一篇树形dp的
    代码: 
    #include <cstdlib>
    #include <cctype>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <sstream>
    #include <map>
    #include <set>
    #include <queue>
    #include <stack>
    #include <fstream>
    #include <numeric>
    #include <iomanip>
    #include <bitset>
    #include <list>
    #include <stdexcept>
    #include <functional>
    #include <utility>
    #include <ctime>
    
    #define PB push_back
    #define MP make_pair
    using namespace std;
    typedef long long LL;
    #define PI acos((double)-1)
    #define E exp(double(1))
    const int K=1500+2;
    short vis[K],head[K],cnt;
    struct Edge
    {
        short next,to;
    }edge[K*2];
    void add(int u,int v)
    {
        edge[cnt].next=head[u];
        edge[cnt].to=v;
        head[u]=cnt++;
    }
    void dfs(int x,int &v1,int &v2)
    {
        v1=1;v2=0;
        vis[x]=1;
        int x1,x2;
        for(int i=head[x];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(vis[v])continue;
            dfs(v,x1,x2);
            v1+=min(x1,x2);
            v2+=x1;
        }
    }
    int main(void)
    {
        int n;
        while(~scanf("%d",&n))
        {
           cnt=0;
           memset(head,-1,sizeof(head));
           memset(vis,0,sizeof(vis));
           for(int i=1;i<=n;i++)
           {
               int u,v,k;
               scanf("%d:(%d)",&u,&k);
               while(k--)
                scanf("%d",&v),add(u,v),add(v,u);
           }
           int x,y;
           dfs(0,x,y);
           printf("%d
    ",min(x,y));
        }
        return 0;
    }
  • 相关阅读:
    openwrt官方固件怎么中继网络
    hibernate 映射一对多
    jsp连接access数据库
    表单判断值是否为空
    eclipse java ee jsp tomcat Server文件夹给删了,怎么办?
    Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class java.lang.Long
    myeclipse创建hibernate工程
    Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
    Mac 挂载树莓派nfs
    树莓派集群实践——nfs
  • 原文地址:https://www.cnblogs.com/weeping/p/5826797.html
Copyright © 2011-2022 走看看