思路:就是找能走的走,遍历一边所有情况,满足就退出。
Accepted | 4284 | 328MS | 2280K | 2239 B | C++ |
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <algorithm>
#include <vector>
#define Maxn 110
#define Maxm 6000
#define LL int
#define inf 100000000
#define Abs(a) (a)>0?(a):(-a)
using namespace std;
int D[Maxn],C[Maxn],dis[Maxn][Maxn],choice[Maxn],n,m,h,vi[Maxn];
void init()
{
memset(D,,sizeof(D));
memset(C,,sizeof(C));
memset(vi,,sizeof(vi));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
dis[i][j]=inf;
}
void floyd()
{
int i,j,k;
for(i=;i<=n;i++)
dis[i][i]=;
for(k=;k<=n;k++)
for(i=;i<=n;i++)
for(j=;j<=n;j++)
dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]);
}
int dfs(int u,int leave,int num)
{
int i,j;
if(num==h)
{
if(dis[u][]<=leave)
return ;
return ;
}
for(i=;i<=h;i++)
if(!vi[choice[i]])
{
if(leave>=dis[u][choice[i]]+D[choice[i]])
{
vi[choice[i]]=;
if(dfs(choice[i],leave-dis[u][choice[i]]-D[choice[i]]+C[choice[i]],num+))
return ;
vi[choice[i]]=;
}
}
return ;
}
int main()
{
int i,j,a,b,c,t,money;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d%d",&n,&m,&money);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
dis[a][b]=dis[b][a]=c<dis[a][b]?c:dis[a][b];
}
floyd();
scanf("%d",&h);
for(i=;i<=h;i++)
{
scanf("%d%d%d",&a,&b,&c);
choice[i]=a,C[a]=b,D[a]=c;
}
for(i=;i<=h;i++)
{
if(!vi[choice[i]])
{
if(money>=dis[][choice[i]]+D[choice[i]])
{
vi[choice[i]]=;
if(dfs(choice[i],money-dis[][choice[i]]-D[choice[i]]+C[choice[i]],))
break;
vi[choice[i]]=;
}
}
}
if(i<=h)
printf("YES\n");
else
printf("NO\n");
}
return ;
}