TravelTime Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3830 Accepted Submission(s): 996
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
Sample Output
Source
Recommend
| ||
|
这题有毒,一开始走得到1这个点的工作证可能不是最优的,坑我好久
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define bug printf("hihi\n")
#define eps 1e-12
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define N 105
int a[N][N];
int out[N],in[N];
int n,m,money;
int dp[1<<17][17];
vector<int>g;
void floyd()
{
int i,j,k;
for(k=0;k<=n;k++)
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
a[i][j]=min(a[i][k]+a[k][j],a[i][j]);
}
bool solve()
{
floyd();
int i,j;
for(i=0;i<g.size();i++) //不一定一开始就得到1这个点的工作证
{
if(money>=a[0][g[i]]+out[g[i]])
dp[1<<i][i]=money-a[0][g[i]]-out[g[i]]+in[g[i]];
}
int k=g.size()-1;
int len=1<<g.size();
for(int cur=0;cur<len;cur++)
for(int i=0;i<=k;i++)
{
if(!(cur&(1<<i))) continue;
if(dp[cur][i]<0) continue;
for(j=0;j<=k;j++)
{
if(cur&(1<<j)) continue;
if(dp[cur][i]<out[g[j]]+a[g[i]][g[j]]) continue;
int to=cur|(1<<j);
dp[to][j]=max(dp[to][j],dp[cur][i]+in[g[j]]-out[g[j]]-a[g[i]][g[j]]);
}
}
for(i=0;i<=k;i++)
if(dp[len-1][i]>=a[g[i]][1]) return true;
return false;
}
int main()
{
int i,j,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&money);
memset(a,INF,sizeof(a));
int u,v,len;
while(m--)
{
scanf("%d%d%d",&u,&v,&len);
if(a[u][v]>len) a[u][v]=a[v][u]=len;
}
for(i=0;i<=n;i++)
a[i][i]=0;
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
g.clear();
int h;
scanf("%d",&h);
int pos=-1;
while(h--)
{
scanf("%d",&u);
scanf("%d%d",&in[u],&out[u]);
g.push_back(u);
if(u==1) pos=1;
}
if(pos==-1)
{
g.push_back(1);
pos=g.size()-1;
}
memset(dp,-1,sizeof(dp));
sort(g.begin(),g.end());
a[0][1]=a[1][0]=0;
if(solve())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
/*
4
2 1 100
1 2 10000
1
2 100000 1
*/