hdu4284之状态压缩dp
2014-04-02 09:30
369 查看
倒计时第5天!——携程编程大赛,你报名了吗? |
TravelTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2773 Accepted Submission(s): 760 Problem Description PP loves travel. Her dream is to travel around country A which consists of N cities and M roads connecting them. PP has measured the money each road costs. But she still has one more problem: she doesn't have enough money. So she must work during her travel. She has chosen some cities that she must visit and stay to work. In City_i she can do some work to earn Ci money, but before that she has to pay Di money to get the work license. She can't work in that city if she doesn't get the license but she can go through the city without license. In each chosen city, PP can only earn money and get license once. In other cities, she will not earn or pay money so that you can consider Ci=Di=0. Please help her make a plan to visit all chosen cities and get license in all of them under all rules above. PP lives in city 1, and she will start her journey from city 1. and end her journey at city 1 too. Input The first line of input consists of one integer T which means T cases will follow. Then follows T cases, each of which begins with three integers: the number of cities N (N <= 100) , number of roads M (M <= 5000) and her initiative money Money (Money <= 10^5) . Then follows M lines. Each contains three integers u, v, w, which means there is a road between city u and city v and the cost is w. u and v are between 1 and N (inclusive), w <= 10^5. Then follows a integer H (H <= 15) , which is the number of chosen cities. Then follows H lines. Each contains three integers Num, Ci, Di, which means the i_th chosen city number and Ci, Di described above.(Ci, Di <= 10^5) Output If PP can visit all chosen cities and get all licenses, output "YES", otherwise output "NO". Sample Input 2 4 5 10 1 2 1 2 3 2 1 3 2 1 4 1 3 4 2 3 1 8 5 2 5 2 3 10 1 2 1 100 1 2 10000 1 2 100000 1 Sample Output YES NO |
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <iomanip> #define INF 99999999 typedef long long LL; using namespace std; const int MAX=(1<<16)+10; int n,m,val,h; int s[20],c[20],d[20]; int dp[MAX][20],dist[110][110]; void Init(int num){ for(int i=0;i<=num;++i){ for(int j=i+1;j<=num;++j)dist[i][j]=dist[j][i]=INF; } } void floyd(){ for(int k=1;k<=n;++k){ for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]); } } } } void DP(){ int bit=1<<(h+1); memset(dp,-1,sizeof dp); dp[1][0]=val; for(int i=1;i<bit;++i){ for(int j=0;j<=h;++j){ if(dp[i][j] == -1)continue; for(int k=1;k<=h;++k){ if(dp[i][j]<dist[s[j]][s[k]]+d[k])continue; int p=1<<k,w=c[k]-d[k]; if(i&p)continue; dp[i|p][k]=max(dp[i|p][k],dp[i][j]-dist[s[j]][s[k]]+w); } } } bool flag=false; for(int i=0;i<=h;++i)if(dp[bit-1][i]-dist[s[i]][1]>=0)flag=true; if(flag)printf("YES\n"); else printf("NO\n"); } int main(){ int t,u,v,w; scanf("%d",&t); while(t--){ scanf("%d%d%d",&n,&m,&val); Init(n); for(int i=0;i<m;++i){ scanf("%d%d%d",&u,&v,&w); dist[u][v]=dist[v][u]=min(dist[u][v],w); } scanf("%d",&h); for(int i=1;i<=h;++i){ scanf("%d%d%d",&s[i],&c[i],&d[i]); } floyd(); s[0]=1,c[0]=d[0]=0; DP(); } return 0; }
相关文章推荐
- HDU 4284 Travel(状态压缩DP)
- HDU 4284 Travel / 状态压缩DP
- HDU 4284 Travel【状态压缩DP】
- HDU 4284(状态压缩dp)
- hdu 4284 状态压缩dp
- hdu 4906 Our happy ending 状态压缩dp 较难
- hdu-4539-郑厂长系列故事――排兵布阵 状态压缩dp
- HDU--1074(状态压缩DP)
- HDU1074 Doing Homework 状态压缩DP
- HDU4539+状态压缩DP
- HDU 4906 状态压缩dp
- Hdu-1565 方格取数(1) (状态压缩dp入门题
- hdu 2167 Pebbles(状态压缩DP)
- hdu1074 状态压缩dp 记录路径
- hdu 4539(状态压缩dp)
- hdu 4906 Our happy ending 状态压缩dp
- HDU 3247 AC自动机 + 状态压缩dp
- [ACM] hdu 5045 Contest (状态压缩Dp)
- hdu 1565 方格取数(1) (状态压缩DP)
- HDU 3001 Travelling(状态压缩dp)