HDOJ 4284 Travel (DFS + Floyd )

来源:互联网 发布:如何传软件 编辑:程序博客网 时间:2024/04/28 19:50


Travel

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4338    Accepted Submission(s): 1165


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

24 5 101 2 12 3 21 3 21 4 13 4 231 8 52 5 23 10 12 1 1001 2 1000012 100000 1
 

Sample Output

YESNO


#include <bits/stdc++.h>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define f(i,a,b) for(int i=(a);i<=(b);++i)const int maxn = 105;const int mod = 10005;const int INF = 1e9;#define ll long long#define rush() int T;scanf("%d",&T);while(T--)int dis[maxn][maxn];bool vis[maxn];int n,m,s,h;int flag;struct node{    int id,c,d;}mp[20];void init(){    f(i,1,n)    f(j,1,n)    {        if(i==j)            dis[i][j]=0;        else dis[i][j]=INF;    }}void floyd(){    f(k,1,n)    f(i,1,n)    {        if(dis[i][k]!=INF)        {            f(j,1,n)            {                if(dis[i][k]+dis[k][j]<dis[i][j])                    dis[i][j]=dis[i][k]+dis[k][j];            }        }    }}void dfs(int x,int num,int s){    if(flag)        return;    if(num==h)    {        if(s-dis[x][1]>=0)        {            flag=1;        }        return;    }    f(i,1,h)    {        if(vis[mp[i].id]==0&&dis[x][mp[i].id]!=INF)        {            if(s-dis[x][mp[i].id]>=mp[i].d)            {                vis[mp[i].id]=1;                dfs(mp[i].id,num+1,s-dis[x][mp[i].id]-mp[i].d+mp[i].c);                if(flag)                    return;                vis[mp[i].id]=0;            }        }    }}int main(){    int x,y,w;    rush()    {        scanf("%d%d%d",&n,&m,&s);        init();        f(i,1,m)        {            scanf("%d%d%d",&x,&y,&w);            if(dis[x][y]>w)                dis[x][y]=dis[y][x]=w;        }        floyd();        scanf("%d",&h);        f(i,1,h)        {            scanf("%d%d%d",&mp[i].id,&mp[i].c,&mp[i].d);        }        flag=0;        mst(vis,0);        dfs(1,0,s);        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;}



0 0
原创粉丝点击