hdu 4640 Island and study-sister(压缩DP+最短路,5级)
来源:互联网 发布:oanda 数据接口 编辑:程序博客网 时间:2024/06/08 06:35
Island and study-sister
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 236 Accepted Submission(s): 70
Problem Description
Members of ACM/ICPC teams of Fuzhou University always stay in the laboratory in their free time. One day the team members of OOXX are doing their daily training and suddenly received k calls from study-sisters asking for their help. You can regard the campus of Fuzhou University is consist of n beautiful islands and connected by m undirection bridges and the study-sisters are in some of these islands waiting for them. As the members of OOXX are all warm-hearted, they don’t want these study-sisters waiting too long and just want the time the girl whom waiting for the longest be as short as possible. You can assume that 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 island can be visited only by one member except the laboratory. This means that even if two guys come to an island in two different times is forbidden. Now your task is calculating how long these study-sisters will wait. Note that there are three students in team OOXX.
But due to some mysterious reason, each island can be visited only by one member except the laboratory. This means that even if two guys come to an island in two different times is forbidden. Now your task is calculating how long these study-sisters will wait. Note that there are three students in team OOXX.
Input
The first line contains only one integer T (T<=150), which is the number of test cases. Each test case contains two integer n (1<= n <=17), m (m <= n*n), means that Fuzhou university is consist of n islands and there are m undirection bridges connect them. Then comes m lines, each line contains three integer x, y, s, means that there is a bridge connect island x and island y and it takes s unit of time to go through this bridge. The numbers start from 1 to n and the number of the laboratory is always 1. Then comes a number k (k>=1) indicate that there are k study-sisters waiting for help. The next line are k integers xi (2<=xi<=n) describe where these study-sisters are. You can assume that all the study-sisters are in different islands.
Output
For each test case, output the case number first, then output the time 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
Source
2013 Multi-University Training Contest 4
Recommend
zhuyuanchen520
思路:先找出最短路,然后状态压缩DP一下。
坑:不能全部初始化,会超
#include<cstdio>#include<cstring>#include<queue>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define ll(z) (1<<z)using namespace std;const int mm=18;const int oo=0x3f3f3f3f;int dis[mm][ll(mm)],dp[2][ll(mm)];int head[mm],edge,target;bool vis[mm][ll(mm)];class Edge{ public:int v,next,w;}e[mm*mm*2];int cas,n,m;void data(){ clr(head,-1);edge=0;}void add(int u,int v,int w){ e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++; e[edge].v=u;e[edge].w=w;e[edge].next=head[v];head[v]=edge++;}void spfa(){ queue<pair<int,int > >Q; // clr(dis,0x3f);clr(vis,0); FOR(i,0,ll(n)-1)FOR(j,0,n)dis[j][i]=oo,vis[j][i]=0; Q.push(make_pair(0,1) );dis[0][1]=0; int u,v,state,snew; pair<int,int>z; while(!Q.empty()) { z=Q.front();Q.pop(); u=z.first;state=z.second;vis[u][state]=0; for(int i=head[u];~i;i=e[i].next) { v=e[i].v;snew=state|ll(v); if(dis[v][snew]>dis[u][state]+e[i].w) { dis[v][snew]=dis[u][state]+e[i].w; if(!vis[v][snew]) {Q.push(make_pair(v,snew));vis[v][snew]=1;} } } } // clr(dp,0x3f); FOR(i,2,ll(n)-1) {dp[0][i>>1]=oo; FOR(j,1,n) { dp[0][i>>1]=min(dp[0][i>>1],dis[j][i|1] ); //printf("ddis= %d %d\n",dp[0][i],i); } } // FOR(i,0,ll(n)-1)cout<<dp[0][i]<<endl; dp[0][0]=0;///important}void DP(){ n--; FOR(i,0,ll(n)-1) {dp[1][i]=oo; for(int j=i;j>0;j=(j-1)&i) { if(dp[0][i^j]!=oo && dp[0][j]!=oo ) { dp[1][i]=min(dp[1][i],max(dp[0][i^j],dp[0][j]) ); } } } //FOR(i,0,ll(n)-1) // cout<<dp[1][i]<<endl; int ans=oo; FOR(i,0,ll(n)-1) for(int j=i;j>0;j=(j-1)&i) { if((i&target)==target) if(dp[0][i^j]!=oo && dp[1][j]!=oo ) { //printf("pp= %d %d\n",dp[0][i^j],dp[1][j]); ans=min(ans,max(dp[0][i^j],dp[1][j]) ); } } // FOR(i,0,ll(n)-1)//cout<<dp[2][i]<<endl; // if((i&target)==target) // ans=min(ans,dp[2][i]); if(ans==oo)printf("-1\n"); else printf("%d\n",ans);}int main(){ int K,a,b,c; //freopen("1009.in","r",stdin); while(~scanf("%d",&cas)) { FOR(ca,1,cas) { printf("Case %d: ",ca); data(); scanf("%d%d",&n,&m); FOR(i,1,m) { scanf("%d%d%d",&a,&b,&c);--a;--b;add(a,b,c); } scanf("%d",&K); target=0; FOR(i,1,K) { scanf("%d",&a);a-=2;target|=ll(a); } // printf("tt=%d\n",target); spfa();DP(); } } return 0;}
- hdu 4640 Island and study-sister(压缩DP+最短路,5级)
- hdu 4640 Island and study-sister(状态压缩dp)
- hdu 4640 Island and study-sister(最短路+状压dp)
- HDU 4640 Island and study-sister(状态压缩DP+路径压缩)经典 旅行商问题
- hdu 4640 Island and study-sister
- hdu 4640 Island and study-sister 13多校,难
- hdu 5418 Victor and World 状态压缩dp spfa最短路 floyed最短路
- HDU-4568 Hunter 最短路 + 状态压缩DP(TSP)
- hdu 3247(ac自动机+状态压缩dp+最短路)
- HDU Victor and World (最短路+状态压缩)
- hdu 5418 Victor and World 状态压缩+SPFA最短路
- hdu 4640 多校第四场 状态压缩+最短路+dp
- HDU 5418 Victor and World(状压dp、最短路)
- HDU 2416 Treasure of the Chimp Island bfs 最短路
- Hdu 4634 DP|最短路
- 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter
- hdu 4114 状态压缩+最短路
- HDU 5418 Victor and World(状压dp、floy最短路)
- 执行Android JUnit测试出现java.net.SocketException: Permission denied的解决方法
- android的frameworks层键盘事件处理流程分析
- 吐槽1--弄了一个下午的黑苹果,结果还是以失败告终
- 韩信点兵
- 嵌入式面试题(转)
- hdu 4640 Island and study-sister(压缩DP+最短路,5级)
- Cloud Foundry中 JasperReports service集成
- linux主机改名以及oracle启动问题
- 解决学生信息管理系统之List列表中重复添加相同数据问题。
- u-boot串口初始化全过程
- HDOJ 4628 - Pieces 状态压缩DP..枚举所有子集进行更新
- 容斥原理应用(求1~r中有多少个数与n互素)
- mysql pod 插入 乱码的解决方案
- 排序算法(个人总结)