这题对于状态要理解一点,路过和在一个地点打工的区别。先用floyd处理两个点之间的最小花费。另外这一题要注意,每次到一个城市打工的时候要判断剩余的钱是否足够支付打工前的费用。
状态转移方程dp[s][i]=max(dp[s][i],dp[s'][j]-maps[j][i]-d[i]+c[i]);
dp[s][i]表示当在状态s的时候最后再i城市打工的最多剩余钱数。
最后我们判断dp【(1<<k)-1】【i】-maps【i】【1】是否有解就可以判断是否存在方案了。和POJ那道题目其实很像,现在想想看这一题其实很基础。
Travel
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1900 Accepted Submission(s): 542
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.
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)
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<cstring>
#include<bitset>
#include<algorithm>
using namespace std;
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define INF 0xFFFFFF
#define MAXN 110
int dp[1<<16][16];
int maps[MAXN][MAXN];
int h[16],c[16],d[16];
int n,m,k,f;
void floyd()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
{
if(k==i || maps[i][k]==INF) continue;
for(int j=1;j<=n;j++)
{
if(j==i) continue;
if(maps[k][j]==INF) continue;
maps[i][j]=min(maps[i][j],maps[i][k]+maps[k][j]);
}
}
}
void solve()
{
int x,y,z,tmp;
scanf("%d%d%d",&n,&m,&f);
//memset(maps,INF,sizeof(maps));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
maps[i][j]=INF;
for(int i=1;i<=n;i++)
maps[i][i]=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if(maps[x][y]>z)
maps[x][y]=maps[y][x]=z;
}
floyd();
scanf("%d",&k);
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&h[i],&c[i],&d[i]);
if(h[i]==1)
{
swap(h[i],h[1]);
swap(c[i],c[1]);
swap(d[i],d[1]);
}
}
memset(dp,-1,sizeof(dp));
for(int i=1;i<=k;i++)
{
int s=0;
s|=(1<<(i-1));
if(f>=maps[1][h[i]]+d[i])
dp[s][i]=f-maps[1][h[i]]-d[i]+c[i];
}
for(int s=0;s<(1<<k);s++)
{
for(int i=1;i<=k;i++)
{
if(!(s&(1<<(i-1)))) continue;
for(int j=1;j<=k;j++)
{
if(maps[h[j]][h[i]]==INF) continue;
if( !(s&(1<<(j-1))) || j==i) continue;
tmp=s^(1<<(i-1));
if(dp[tmp][j]==-1) continue;
if(dp[tmp][j]>=maps[h[j]][h[i]]+d[i])
dp[s][i]=max(dp[s][i],dp[tmp][j]-maps[h[j]][h[i]]-d[i]+c[i]);
}
}
}
int ans=-1;
for(int i=1;i<=k;i++)
ans=max(ans,dp[(1<<k)-1][i]-maps[h[i]][1]);
if(ans>=0)
printf("YES\n");
else
printf("NO\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
solve();
return 0;
}