hdu1224 Free DIY Tour--DP
2016-03-27 15:44
281 查看
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1224
一:原题内容
[align=left]Problem Description[/align]
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 both
1 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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3
0 70 90
4
1 2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
[align=left]Sample Output[/align]
CASE 1#
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
二:分析理解
设 dp[i] 为从城市 1 到城市 i 所能够获得的最大 interesting point
三:AC代码
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int visit[110][110];
int pre[110];
int points[110];
int dp[110];
void Out(int p)
{
if (p == 1)
{
printf("1->");
return;
}
Out(pre[p]);
printf("%d->", p);
}
int main()
{
int T;
int N;
int M;
int x, y;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
if (cas != 1)
printf("\n");
memset(visit, 0, sizeof(visit));//
memset(points, 0, sizeof(points));//
memset(dp, 0, sizeof(dp));//
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%d", &points[i]);
scanf("%d", &M);
for (int i = 0; i < M; i++)
{
scanf("%d %d", &x, &y);
visit[x][y] = 1;
}
dp[N + 1] = 0;//
for (int i = 1; i <= N + 1; i++)
{
for (int j = 0; j < i; j++)
{
if (visit[j][i])
{
if (dp[i] < dp[j] + points[i])
{
dp[i] = dp[j] + points[i];
pre[i] = j;
}
}
}
}
printf("CASE %d#\n", cas);
printf("points : %d\n", dp[N + 1]);
printf("circuit : ");
Out(pre[N + 1]);
printf("1\n");
}
return 0;
}
一:原题内容
[align=left]Problem Description[/align]
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 both
1 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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
3
0 70 90
4
1 2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
[align=left]Sample Output[/align]
CASE 1#
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
二:分析理解
设 dp[i] 为从城市 1 到城市 i 所能够获得的最大 interesting point
三:AC代码
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
int visit[110][110];
int pre[110];
int points[110];
int dp[110];
void Out(int p)
{
if (p == 1)
{
printf("1->");
return;
}
Out(pre[p]);
printf("%d->", p);
}
int main()
{
int T;
int N;
int M;
int x, y;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
if (cas != 1)
printf("\n");
memset(visit, 0, sizeof(visit));//
memset(points, 0, sizeof(points));//
memset(dp, 0, sizeof(dp));//
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%d", &points[i]);
scanf("%d", &M);
for (int i = 0; i < M; i++)
{
scanf("%d %d", &x, &y);
visit[x][y] = 1;
}
dp[N + 1] = 0;//
for (int i = 1; i <= N + 1; i++)
{
for (int j = 0; j < i; j++)
{
if (visit[j][i])
{
if (dp[i] < dp[j] + points[i])
{
dp[i] = dp[j] + points[i];
pre[i] = j;
}
}
}
}
printf("CASE %d#\n", cas);
printf("points : %d\n", dp[N + 1]);
printf("circuit : ");
Out(pre[N + 1]);
printf("1\n");
}
return 0;
}
相关文章推荐
- [ZJOI2012]网络 解题报告
- 【追求进步】栈的压入、弹出序列
- Java_Servlet笔记:转发与重定向
- 第五周醒目一 三角形类雏形(3)
- C++中vector的remove用法
- 为VirtualBox里的Linux系统安装增强功能
- Volley框架源码分析
- 已经开发好的网站,进行多语言支持
- 淘宝的SEM如何操作及“车神”的由来太原网络营销师揭秘
- 去掉listview 的点击效果
- javascript-知识点集合
- 敏捷开发方法综述
- iphone5s如何省电?耗电费电如何处理?iphone5s耗电费电解决方法
- Spring的设计目标
- Sublime Text 中使用Markdown
- 第五周实践项目1————三角形的雏形(3)
- Git push时重复输入用户名密码的问题
- UML基础知识点
- 解决 IDM 注册问题(非破解)
- 浅谈属性动画的简单使用(一)