HDU 4640 Island and Study-sister (state compression dp+ path compression) Classic Travel business Questions

Source: Internet
Author: User
Island and Study-sisterTime limit:4000/2000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 790 Accepted Submission (s): 273


Problem descriptionmembers of ACM/ICPC teams of Fuzhou University always stay in the laboratory in their free time. One day the team members of Ooxx is doing their daily training and suddenly received K calls from Study-sisters asking fo R their help. You can regard the campus of Fuzhou University are consist of n beautiful islands and connected by M undirection Bridges an D The study-sisters is in some of these islands waiting for them. As the members of Ooxx is all warm-hearted, they don ' t want these study-sisters waiting too long and just want the time t He girl whom waiting for the longest is as short as possible. You can assume this they begin to move immediately after they received the calls. They start from the laboratory and they can go anywhere freely by the bridges.
But due to some mysterious reason, each of the island can is visited by one member except the laboratory. This means, that even if the guys come to a island in the different times is forbidden. Now your task was calculating how long these study-sisters would wait. Note that there is three students in Team Ooxx.
Inputthe first line contains only one integer T (t<=150), which is the number of the test cases. Each test case contains the 1<= n <=17, m (M <= n*n), means that Fuzhou University are consist of n is Lands and there are M undirection bridges connect them. Then comes m lines, each line contains three integer x, y, s, means that there are a bridge connect island X and Island y a nd it takes s unit of time to go through this bridge. The numbers start from 1 to N and the number of the laboratory are always 1. Then comes a number K (k>=1) indicate that there is K study-sisters waiting for help. The next line is K integers XI (2<=xi<=n) describe where these study-sisters is. You can assume this all the study-sisters is in different islands.
Outputfor each test case, output the case number first, and then output the time of the girl whom wait for the longest wait. You should make this number as small as possible. If any one of the study-sisters can ' t get help, just output-1.
Sample Input
42 0122 11 2 1124 31 2 12 3 22 4 223 44 31 2 21 3 31 4 432 3 4

Sample Output
Case 1: -1case 2:1case 3:7case 4:4

Source2013 multi-university Training Contest 4 test instructions: to an no-map (n<=17), M-Edge, there are three people from 1 points, to go to K city, by three people walk, everyone through the city can not be the same except 1 points, Some people can stop at 1 o ' walk, ask three people through all the city point must go through that K city, three people spend the smallest of what is the maximum value. Problem solving: The first path to compress the state of the city to spend the least, to find out all possible state costs, which is a person's spending. Then use the 01 backpack method to find out the minimum cost of the total state of the city of three people (be sure to note: All start from 1 o'clock). Finally, we find the answer that satisfies the condition.
#include <string.h> #include <stdio.h> #include <queue>using namespace std;const int inf = 1<<29; const int N = 17;struct edg{int to,next,cost;} Edg[n*n];int Eid,head[n];queue<int>id[1<<n];int dp[1<<n][n],vist[1<<n][n],mincost[1<    <n],f[1<<n];void addedg (int u,int v,int c) {edg[eid].to=v;    Edg[eid].cost=c;    Edg[eid].next=head[u]; head[u]=eid++;}    int main () {int t,t=0,n,m,a,b,c,mapt[n][n];    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        Eid=0;        memset (head,-1,sizeof (head));        memset (vist,0,sizeof (vist));        for (int st=1; st< (1<<n), st++) for (int i=0; i<n; i++) Dp[st][i]=inf;        for (int i=0, i<n; i++) for (int j=0; j<n; j + +) Mapt[i][j]=inf;            while (m--) {scanf ("%d%d%d", &a,&b,&c); a--;            b--;        if (mapt[a][b]>c) mapt[a][b]=mapt[b][a]=c;  }      for (int i=0, i<n; i++) for (int j=0; j<n; J + +) if (mapt[i][j]!=inf) ad        DEDG (I,j,mapt[i][j]);        int flag=0;        scanf ("%d", &m);        while (m--) {scanf ("%d", &a); a--; flag|=1<<a;        }//path compression DP dp[1][0]=0;        Id[1].push (0);        Vist[1][0]=1;                for (int st=1; st< (1<<n), st++) {while (!id[st].empty ()) {int U=id[st].front ();                Id[st].pop ();                vist[st][u]=0;                    for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to; if (dp[st| ( 1&LT;&LT;V) (][v]>dp[st][u]+edg[i].cost) {dp[st| (                        1&LT;&LT;V)][v] = Dp[st][u]+edg[i].cost; if (vist[st| ( 1&LT;&LT;V)][v]==0) vist[st| ( 1&LT;&LT;V)][v]=1,id[st| (                    1&LT;&LT;V)].push (v); }}}} for (int st=1;st< (1<<n);            st++)//Find out a minimum cost to reach the city state {Mincost[st]=inf;            BOOL in=0; for (int i=0; (1<<i) <=st;                 i++) if ((st& (1<<i)) &&dp[st][i]!=inf) {if (Mincost[st]>dp[st][i])            Mincost[st]=dp[st][i];        } F[st]=mincost[st]; }//With 01 backpack method for (int k=1, k<3; k++) for (int st= (1<<n)-1; st>0; st--) if (St            &AMP;1)//must have 1 points for (int s1=st; s1>0; s1= (s1-1) &st) {int s2=st^s1; S2|=1;            Guaranteed to depart from 1 pips int tmp=f[s1|1];            if (Tmp<mincost[s2]) TMP=MINCOST[S2];        if (f[st]>tmp) f[st]=tmp;        } int ans=inf; for (int st=1; st< (1<<n); st++) if ((St&flag) ==flag) {if (ans>f[st]) ans=f[s        T];        } if (Ans==inf) ans=-1;    printf ("Case%d:%d\n", ++t,ans); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 4640 Island and Study-sister (state compression dp+ path compression) Classic Travel business Questions

Related Article
Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.