HDU - 4284 Travel(状压dp)

 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


状压dp,可惜并不太会状压dp

/*状压DP*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
typedef long double LD;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = (1<<16)+10;
int N,M,T,Money,H;
int Map[110][110];
int dp[maxn][20];
struct must{
	int Ci,Di,id;
}Nodes[20];
void Init(){//Floyd	求任意两点间代价
	for(int i = 1; i <= N; i++)
		Map[i][i] = 0;
	for(int k = 1; k <= N; k++)
		for(int i = 1; i <= N; i++)
			for(int j = i+1; j <= N; j++)
				Map[j][i] = Map[i][j] = min(Map[i][j],Map[i][k]+Map[k][j]);//地点i到达地点j需要花费Map[i][j];
}
void dp_mmp(){
	int total = 1<<(H+1);
	memset(dp,-1,sizeof(dp));
	dp[1][0] = Money;//最初的
	for(int i = 1; i < total; 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] < Map[Nodes[j].id][Nodes[k].id]+Nodes[k].Di)//钱不够买签证
					continue;
				int temp = 1<<k;
				int  OMG = Nodes[k].Ci-Nodes[k].Di;//买了签证,打完工钱的变化
				if(i&temp)continue;
				dp[i|temp][k] = max(dp[i|temp][k],dp[i][j]-Map[Nodes[j].id][Nodes[k].id]+OMG);
			}
		}
	}
	bool flag = false;
	for(int i = 0; i <= H; i++){
		if(dp[total-1][i]-Map[Nodes[i].id][1] >= 0)
			flag = true;
	}
	if(flag)
		puts("YES");
	else
		puts("NO");
}
int main(){
	int u,v,w;
	scanf("%d",&T);
	while(T--){
		memset(Map,inf,sizeof(Map));
		scanf("%d %d %d",&N,&M,&Money);
		for(int i = 0; i < M; i++){
			scanf("%d %d %d",&u,&v,&w);
			Map[u][v] = Map[v][u] = min(Map[u][v],w);
		}
		Init();
		scanf("%d",&H);
		for(int i = 1; i <= H; i++)
			scanf("%d %d %d",&Nodes[i].id,&Nodes[i].Ci,&Nodes[i].Di);
		Nodes[0].id = 1;
		Nodes[0].Ci = Nodes[0].Di = 0;//避免第一个点也是必须取得点
		dp_mmp();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值