HDU 4085 Stanner tree Template

Source: Internet
Author: User
Dig the Wells Time Limit: 6000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 971 accepted submission (s): 416


Problem descriptionyou may all know the famous story "three monks ". recently they find some places around their temples can been used to dig some wells. it will help them save a lot of time. but to dig the well or build the road to transport the water will cost money. they do not want to cost too much money. now they want you to find a cheapest plan.
Inputthere are several test cases.
Each test case will starts with three numbers n, m, and P in one line, N stands for the number of monks and M stands for the number of places that can been used, P stands for the number of roads between these places. the places the monks stay is signed from 1 to n then the other M places are signed as n + 1 to n + M. (1 <= n <= 5, 0 <= m <= 1000, 0 <= P <= 5000)
Then N + M numbers followed which stands for the value of digging a well in the ith place.
Then P lines followed. Each line will contains three numbers A, B, and C. Means build a road between A and B will cost C.
 
Outputfor each case, output the minimum result you can get in one line.
Sample Input
3 1 31 2 3 41 4 22 4 23 4 4 4 1 45 5 5 5 11 5 12 5 13 5 14 5 1
 
Sample output
65


Question: There are n monks, each monk has a temple, M villages, and P roads. Each road has a cost, and it is also required for Drilling in every place, the minimum amount of money can be spent to make all monks drink water.

Stanna tree is bare.

Code:

/* ***********************************************Author :rabbitCreated Time :2014/7/17 0:59:57File Name :13.cpp************************************************ */#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <iostream>#include <algorithm>#include <sstream>#include <stdlib.h>#include <string.h>#include <limits.h>#include <string>#include <time.h>#include <math.h>#include <queue>#include <stack>#include <set>#include <map>using namespace std;#define INF 100000000#define eps 1e-8#define pi acositypedef long long ll;int head[1100],tol;struct Edge{int next,to,val;}edge[1001000];void addedge(int u,int v,int w){edge[tol].to=v;edge[tol].next=head[u];edge[tol].val=w;head[u]=tol++;}int weight[1100],d[1100][1<<5],dp[1100],in[1010][1<<5];int main(){     //freopen("data.in","r",stdin);     //freopen("data.out","w",stdout);     int n,m,p; while(~scanf("%d%d%d",&n,&m,&p)){ memset(head,-1,sizeof(head));tol=0; for(int i=0;i<n+m;i++) scanf("%d",&weight[i]); while(p--){ int u,v,w; scanf("%d%d%d",&u,&v,&w); u--;v--; addedge(u,v,w); addedge(v,u,w); } for(int i=0;i<n+m;i++) for(int j=0;j<(1<<n);j++) d[i][j]=INF; for(int i=0;i<(1<<n);i++)dp[i]=INF; memset(in,0,sizeof(in)); for(int i=0;i<n;i++) d[i][1<<i]=weight[i]; for(int i=1;i<(1<<n);i++){ queue<int> Q; for(int j=0;j<n+m;j++){ for(int k=i&(i-1);k;k=(k-1)&i) d[j][i]=min(d[j][i],d[j][i-k]+d[j][k]-weight[j]); if(d[j][i]<INF)Q.push(100000*j+i),in[j][i]=1; } while(!Q.empty()){ int v=Q.front()/100000,t=Q.front()%100000; Q.pop(); in[v][t]=0; for(int e=head[v];e!=-1;e=edge[e].next){ int s=edge[e].to; if(d[s][t]>d[v][t]+edge[e].val+weight[s]-weight[v]){ d[s][t]=d[v][t]+edge[e].val+weight[s]-weight[v]; if(!in[s][t]){ in[s][t]=1; Q.push(100000*s+t); } } } } } for(int i=1;i<(1<<n);i++) for(int j=0;j<n+m;j++) dp[i]=min(dp[i],d[j][i]); for(int i=1;i<(1<<n);i++){ for(int j=i&(i-1);j;j=i&(j-1)) dp[i]=min(dp[i],dp[j]+dp[i-j]); } cout<<dp[(1<<n)-1]<<endl; }     return 0;}


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.