标签:树状 -- log nec bitset closed 最优 scanf memory
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1616 | Accepted: 878 |
Description
Input
Output
Sample Input
5 5 1 1 1 1 1 1 1 1 1 1 1 2 1 2 3 1 3 4 1 4 5 1 5 1 1 1 1 1 2 1 1 1 2 1 2 1 2 3 1 3 4 1 4 5 1 5 1 1 3 1 1 2 1 1 1 2 1 2 1 2 3 1 3 4 1 4 5 1 4 2 1 1 1 3 4 3 2 1 2 3 1 3 3 1 4 2 4 4 1 1 1 3 4 3 2 1 2 3 1 3 3 1 4 2
Sample Output
2 1 2 2 3
Source
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<map> #include<stack> #include<set> #include<bitset> using namespace std; #define PI acos(-1.0) #define eps 1e-8 typedef long long ll; typedef pair<int,int > P; const int N=1e3+100,M=2e6+100; const int inf=0x3f3f3f3f; const ll INF=1e13+7,mod=1e9+7; struct edge { int from,to; int w; int next; }; edge es[M]; int cnt,head[N]; int n; int w[N],d[N]; int dist[N][N]; int dp[N][N],ans[N]; void init() { cnt=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int w) { cnt++; es[cnt].from=u,es[cnt].to=v; es[cnt].w=w; es[cnt].next=head[u]; head[u]=cnt; } void bfs(int s) { queue<int>q; dist[s][s]=0; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(dist[s][e.to]>dist[s][u]+e.w) { dist[s][e.to]=dist[s][u]+e.w; q.push(e.to); } } } } void dfs(int u,int fa) { for(int i=head[u]; i!=-1; i=es[i].next) { int v=es[i].to; if(v==fa) continue; dfs(v,u); } for(int v=1; v<=n; v++) { if(dist[u][v]>d[u]) continue; dp[u][v]=w[v]; for(int i=head[u]; i!=-1; i=es[i].next) { edge e=es[i]; if(e.to==fa) continue; dp[u][v]+=min(dp[e.to][v]-w[v],ans[e.to]); } ans[u]=min(ans[u],dp[u][v]); } } int main() { int T; scanf("%d",&T); while(T--) { init(); scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&w[i]); for(int i=1; i<=n; i++) scanf("%d",&d[i]); for(int i=1; i<n; i++) { int u,v,l; scanf("%d%d%d",&u,&v,&l); addedge(u,v,l),addedge(v,u,l); } memset(dist,inf,sizeof(dist)); memset(dp,inf,sizeof(dp)); memset(ans,inf,sizeof(ans)); for(int i=1; i<=n; i++) bfs(i); dfs(1,0); printf("%d\n",ans[1]); } return 0; }
标签:树状 -- log nec bitset closed 最优 scanf memory
原文地址:http://www.cnblogs.com/GeekZRF/p/7631663.html