Travel HDU
来源:互联网 发布:网络打印机添加xp 编辑:程序博客网 时间:2024/06/06 07:42
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
这题没注意有重边,当时看15想过dfs一发,而且点也特变少,不过犹豫了很久,不过dp是真的想到了,但是我想着是递归dp的(和商人旅行的模型是差不多的),但是超内存了,还是在主函数里转移方程。太久没有写过状压dp了,不过dfs的编码复杂度真的简单,噗~
dfs写法:
#include<iostream>#include<cstdio>#include<cstring>#define INF 1000000000using namespace std;int dis[105][105];void floyd(int n){ for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { dis[i][j] =min(dis[i][j], dis[i][k] + dis[k][j]); }}int h;int mapp[18];int C[18];int D[18];bool vis[18];bool sign; void dfs(int cur,int left,int lastP) { if(cur==h+1) { if(left>=dis[lastP][1]) sign=true; return ; } for(int i=1;i<=h;i++) { if(vis[i]) continue; if(left-dis[lastP][mapp[i]]-D[i]>=0&&!sign) { vis[i]=true; dfs(cur+1,left-dis[lastP][mapp[i]]-D[i]+C[i],mapp[i]); vis[i]=false; } } }int main(){ int t; int n,m,money; scanf("%d",&t); int x,y,w; while(t--) { scanf("%d%d%d",&n,&m,&money); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=INF; while(m--) { scanf("%d%d%d",&x,&y,&w); dis[x][y]=dis[y][x]=min(w,dis[y][x]); } for(int i=1;i<=n;i++) dis[i][i]=0; floyd(n); scanf("%d",&h); for(int i=1;i<=h;i++) scanf("%d%d%d",mapp+i,C+i,D+i); sign=false; memset(vis,false,sizeof(vis)); dfs(1,money,1); if(sign) printf("YES\n"); else printf("NO\n"); } return 0;}
dp写法:
#include<iostream>#include<cstdio>#include<cstring>#define INF 1000000000using namespace std;int dis[105][105];void floyd(int n){ for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { dis[i][j] =min(dis[i][j], dis[i][k] + dis[k][j]); }}int dp[(1<<15)+5][16];int mapp[18];int C[18];int D[18];int m;int main(){ int t; int n,money; scanf("%d",&t); int x,y,w; while(t--) { scanf("%d%d%d",&n,&m,&money); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=INF; while(m--) { scanf("%d%d%d",&x,&y,&w); dis[x][y]=dis[y][x]=min(w,dis[y][x]); } for(int i=1;i<=n;i++) dis[i][i]=0; floyd(n); int h; scanf("%d",&h); for(int i=1;i<=h;i++) scanf("%d%d%d",mapp+i,C+i,D+i); memset(dp,-1,sizeof(dp)); for(int i=1;i<=h;i++) { int temp=money-dis[1][mapp[i]]-D[i]; if(temp>=0) dp[1<<(i-1)][i]=temp+C[i]; } int s=(1<<h)-1; for(int i=1;i<=s;i++) { for(int j=1;j<=h;j++) { if(dp[i][j]==-1) continue; for(int k=1;k<=h;k++) { if(i&(1<<(k-1))) continue; int temp=dp[i][j]-dis[mapp[j]][mapp[k]]-D[k]; if(temp>=0) { temp+=C[k]; int s0=i^(1<<(k-1)); dp[s0][k]=max(dp[s0][k],temp); } } } } bool sign=false; for(int i=1;i<=h;i++) { //cout<<dp[s][i]<<endl; if(dp[s][i]>=dis[1][mapp[i]]) { sign=true; break; } } if(sign) printf("YES\n"); else printf("NO\n"); } return 0;}
- hdu Travel
- hdu-travel
- Travel HDU
- Travel HDU
- Travel HDU
- hdu 1208 pascal travel
- HDU 3053 Group Travel
- hdu 4284 Travel
- HDU 4284 Travel
- hdu 2433 TRAVEL
- hdu 4118 Time travel
- HDU 4284 Travel
- hdu 4418 Time travel
- HDU 4284 Travel
- HDU 4418 Time travel
- HDU-5441 Travel
- hdu 5441 Travel
- HDU 5441 Travel
- Android自定义标题+画圆+进度条
- POJ2778 DNA Sequence(AC自动机+矩阵快速幂)
- Java NIO:浅析I/O模型
- Android自带的toolbox分析及扩展
- 【poj 1502】 MPI Maelstrom 【East Central North America 1996】
- Travel HDU
- Can't create handler inside thread that has not called Looper.prepare()
- 数学计算模拟类问题:加法,除法和幂,注意越界问题。题 剑指Offer,Pow(x, n) ,Divide Two Integers
- 菜鸟学框架——Mybatis核心组件
- 4:如何实现对两个整数变量的值进行互换。
- Linux/Centos:如何安装GCC(C和C++编译器)
- pymysql+Flask-SQLAlchemy连接mysql
- 我眼中的数据仓库
- Android_RecycleView基本详解