hdu Free DIY Tour (旅行商问题)
来源:互联网 发布:知网社会与经济数据库 编辑:程序博客网 时间:2024/06/05 19:56
Problem Description
Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is so satisfied with their job that he decide to provide them a free tour around the world. It's a good chance to relax themselves. To most of them, it's the first time to go abroad so they decide to make a collective tour.
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
Input
The input will contain several cases. The first line is an integer T which suggests the number of cases. Then T cases follows.
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
Output
For each case, your task is to output the maximal summation of interesting points Weiwei and his fellow workers can get through optimal DIYing and the optimal circuit. The format is as the sample. You may assume that there is only one optimal circuit.
Output a blank line between two cases.
Output a blank line between two cases.
Sample Input
230 70 9041 21 32 43 430 90 7041 21 32 43 4
Sample Output
CASE 1#points : 90circuit : 1->3->1CASE 2#points : 90circuit : 1->2->1
很容易想到状态转移方程:dp[i]=max(dp[j])+num[i],j是可以到达i的城市,即j是i的前驱,然后用一个数组pre[]记录路径,pre[i]=j
代码:
#include<stdio.h>#include<string.h>int dp[105],num[105],map[105][105],pre[105],rank[105];int main(){ int n,m,k,i,j,t,cas=1,cnt,a,b,flag=0; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(num,0,sizeof(num)); memset(dp,0,sizeof(dp)); memset(map,0,sizeof(map)); for(i=0;i<=n+1;i++) pre[i]=i; for(i=1;i<=n;i++) scanf("%d",&num[i]); scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%d%d",&a,&b); map[a][b]=1; } for(i=2;i<=n+1;i++) for(j=1;j<i;j++) if(map[j][i]) { if(dp[i]<dp[j]+num[i]) { dp[i]=dp[j]+num[i]; pre[i]=j; } } if(flag) printf("\n"); printf("CASE %d#\n",cas++); printf("points : %d\n",dp[n+1]); cnt=0; for(i=n+1;i>1;i=pre[i]) rank[cnt++]=pre[i]; printf("circuit : "); for(i=cnt-1;i>=0;i--) printf("%d->",rank[i]); printf("1\n"); flag=1; } return 0;}
- hdu Free DIY Tour (旅行商问题)
- Free DIY Tour HDU
- hdu 1224 Free DIY Tour
- hdu 1224 Free DIY Tour
- HDU 1224 Free DIY Tour
- hdu 1224 Free DIY Tour
- HDU 1224 Free DIY Tour
- HDU 1224 Free DIY Tour
- hdu 1224 Free DIY Tour
- HDU 1224 Free DIY Tour
- hdu 1224 Free DIY Tour
- hdu 1224 Free DIY Tour
- HDU Free DIY Tour(dp)
- HDU-1224-Free DIY Tour
- hdu 1224 Free DIY Tour
- HDU 1224:Free DIY Tour
- HDU 1224-Free DIY Tour
- HDU 1224 Free DIY Tour
- DirectX11 入门教程 36讲
- jaxb 关于java转xml文章
- 为什么 jdk 中把 String 类设计成 final
- 反for-if编程模式
- 如何在Eclipse中配置Tomcat(免安装版)
- hdu Free DIY Tour (旅行商问题)
- 我们为什么要使用正版软件?
- HDU1856(More is better)—并查集+树
- suse11下开通telnet服务
- 使用WinDBG调试内存泄露的问题
- 大白话解析模拟退火算法
- [開發記錄] 函式庫調用 - GPIO控制 之一
- Web Service学习
- 黑马程序员---正则表达式