【DP】 HDOJ 4284 Travel

2644 단어 dphdoj
먼저 플로이드로 전원 최단로를 미리 처리한 다음에 DP를 누르면 됩니다...
#include <iostream>
#include <queue> 
#include <stack> 
#include <map> 
#include <set> 
#include <bitset> 
#include <cstdio> 
#include <algorithm> 
#include <cstring> 
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 105
#define maxm 10005
#define eps 1e-7
#define mod 1000000007
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
// head

int dp[16][1 << 16];
int g[maxn][maxn];
int a[maxn];
int c[maxn];
int p[maxn];
int d[maxn];
int n, m, o, money;

void init()
{
	memset(g, INF, sizeof g);
}

void read()
{
	int u, v, cc;
	scanf("%d%d%d", &n, &m, &money);
	while(m--) {
		scanf("%d%d%d", &u, &v, &cc);
		g[u][v] = min(g[u][v], cc);
		g[v][u] = min(g[v][u], cc);
	}
	for(int i = 0; i <= n; i++) g[i][i] = 0;
	scanf("%d", &m);
	for(int i = 0; i < m; i++) {
		scanf("%d%d%d", &a[i], &c[i], &d[i]);
		p[i] = 1 << i;
	}
	o = 1 << m;
}

void work()
{
	
	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				g[i][j] = min(g[i][j], g[i][k] + g[k][j]);	
	for(int i = 0; i < m; i++)
		for(int j = 0; j < o; j++)
			dp[i][j] = -INF;
	for(int i = 0; i < m; i++)
		if(money - g[1][a[i]] - d[i] >= 0)
			dp[i][p[i]] = max(dp[i][p[i]], money - g[1][a[i]] - d[i] + c[i]);
	for(int i = 0; i < o; i++)
		for(int j = 0; j < m; j++)
			if(dp[j][i] >= 0)
				for(int k = 0; k < m; k++) {
					if(i & p[k]) continue;
					if(dp[j][i] - d[k] - g[a[j]][a[k]] >= 0)
						dp[k][i | p[k]] = max(dp[k][i | p[k]], dp[j][i] - d[k] - g[a[j]][a[k]] + c[k]);
				}
	int ans = -INF;
	for(int i = 0; i < m; i++) ans = max(ans, dp[i][o - 1] - g[a[i]][1]);
	if(ans >= 0) printf("YES
"); else printf("NO
"); } int main() { int _; scanf("%d", &_); while(_--) { init(); read(); work(); } return 0; }

좋은 웹페이지 즐겨찾기