标签:poj const closed size parent less UI cost push
Description
Input
Output
Sample Input
11 6 1 2 1 3 1 4 1 5 2 6 2 7 2 8 4 9 4 10 4 11
Sample Output
2
Hint
for (int j=m;j>1;j--){ for (int k=1;k<j;k++){ dp[u][j]=min(dp[u][j],dp[u][k]+dp[v[i]][j-k]-2); } }
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<cstdlib> 7 #include<vector> 8 using namespace std; 9 typedef long long ll; 10 typedef long double ld; 11 typedef pair<int,int> pr; 12 const double pi=acos(-1); 13 #define rep(i,a,n) for(int i=a;i<=n;i++) 14 #define per(i,n,a) for(int i=n;i>=a;i--) 15 #define Rep(i,u) for(int i=head[u];i;i=Next[i]) 16 #define clr(a) memset(a,0,sizeof(a)) 17 #define pb push_back 18 #define mp make_pair 19 #define fi first 20 #define sc second 21 #define pq priority_queue 22 #define pqb priority_queue <int, vector<int>, less<int> > 23 #define pqs priority_queue <int, vector<int>, greater<int> > 24 #define vec vector 25 ld eps=1e-9; 26 ll pp=1000000007; 27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;} 28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;} 29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } 30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; } 31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1}; 32 ll read(){ ll ans=0; char last=‘ ‘,ch=getchar(); 33 while(ch<‘0‘ || ch>‘9‘)last=ch,ch=getchar(); 34 while(ch>=‘0‘ && ch<=‘9‘)ans=ans*10+ch-‘0‘,ch=getchar(); 35 if(last==‘-‘)ans=-ans; return ans; 36 } 37 const int M=1000,N=200,inf=1e09; 38 int n,m; 39 int v[M],Next[M],head[N],e,dp[N][N],du[N]; 40 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e;} 41 void dfs(int u,int fa){ 42 for (int i=head[u];i;i=Next[i]) 43 if (v[i]!=fa){ 44 dfs(v[i],u); 45 for (int j=m;j>1;j--){ 46 for (int k=1;k<j;k++){ 47 dp[u][j]=min(dp[u][j],dp[u][k]+dp[v[i]][j-k]-2); 48 } 49 } 50 } 51 } 52 int main(){ 53 n=read(),m=read(); 54 for (int i=1;i<n;i++) { 55 int a=read(),b=read(); 56 add(a,b); add(b,a); 57 du[a]++; du[b]++; 58 } 59 for (int i=1;i<=n;i++){ 60 dp[i][1]=du[i]; 61 for (int j=2;j<=m;j++) 62 dp[i][j]=inf; 63 } 64 dfs(1,0); 65 int ans=inf; 66 for (int i=1;i<=n;i++) 67 ans=min(ans,dp[i][m]); 68 printf("%d",ans); 69 return 0; 70 }
标签:poj const closed size parent less UI cost push
原文地址:http://www.cnblogs.com/SXia/p/7641038.html