您的位置:首页 > 其它

hdu_4284 (Floyd求最短路+dfs遍历)

2017-07-26 21:17 337 查看
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4284


Travel

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4458    Accepted Submission(s): 1200


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

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

题意并不难理解就不过多解释了,需要注意的是每个城市只能赚钱一次和买执照一次,起始的城市是1,如果需要在城市1中获得执照和赚钱的话不一定要先买城市1的只执照and赚城市1的钱。走过H个城市后记得回到城市1,回到城市以的路费也要计算的。

大题思路:先floyd求一下每两个点的最小花费路径,然后dfs求有没有能成功走过这H个城市且回到城市1的情况。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;

struct node
{
int c,d;
int u;
} h[30];
int A[111][111],vis[111],rcd[111],flag;
void init(int n)
{
for(int i=0; i<=n+1; i++)
{
A[i][i]=0;
for(int j=i+1; j<=n+1; j++)
A[i][j]=A[j][i]=INF;
}
memset(vis,0,sizeof(vis));
flag=0;
}
void dfs(int x,int deep,int money,int H)
{
//cout<<x<<" "<<deep<<" "<<money<<endl;
if(flag)return ;
if(deep>=H)
{
if(A[x][1]<INF&&A[x][1]<=money){
flag=1;
}
return ;
}
for(int i=0; i<H; i++)
{
int v=h[i].u;
if(A[x][v]>=INF||vis[v]||money-A[x][v]<h[i].d)continue;
vis[v]=1;
dfs(v,deep+1,money-A[x][v]-h[i].d+h[i].c,H);
vis[v]=0;
}
}
int main()
{
int n,m,money;
int _,u,v,w,H;
scanf("%d",&_);
while(_--)
{
scanf("%d%d%d",&n,&m,&money);
init(n);
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
A[u][v]=A[v][u]=min(A[u][v],w);
}
scanf("%d",&H);
for(int i=0; i<H; i++)
scanf("%d%d%d",&h[i].u,&h[i].c,&h[i].d);
for(int k=1; k<=n; k++)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(A[i][j]>A[i][k]+A[k][j])
A[i][j]=A[i][k]+A[k][j];
}
}
}
dfs(1,0,money,H);///当选择的城市有1的时候,虽然是从1开始走,但是可以不先拿1城市的执照
if(flag)printf("YES\n");
else printf("NO\n");
}
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: