HDU1224

来源:互联网 发布:c语言void 的用法 编辑:程序博客网 时间:2024/06/03 12:27

1.题目描述:

Free DIY Tour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6695    Accepted Submission(s): 2183


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 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?
 

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.
 

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.
 

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
 

Author
JGShining(极光炫影)
 

Source
杭州电子科技大学第三届程序设计大赛
 

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1227 1080 1300 1158 1074 

2.题意概述:

给出一些城市的point,从起点出发经过一些城市最终回到起点,在这个过程中经过的下一个城市的point值要求不低于现在城市的point值(最后回到城市1的情况不算),问最后能够达到的最大point值之和,并输出路径。

3.解题思路:

最长路典型问题,和最短路一样,就是初始化dis为-INF,每次更新dis顺代记录路径,

if (dis[v] < dis[u] + w) dis[v] = dis[u] + w;

然后倒着输出就行。

当然还有一种做法是动态规划,这个代码以后在补dp专题时候再上。

4.AC代码:

#include <bits/stdc++.h>#define INF 0x3f3f3f3f#define maxn 10010#define N 111#define eps 1e-6#define pi acos(-1.0)#define e exp(1.0)using namespace std;const int mod = 1e9 + 7;typedef long long ll;typedef unsigned long long ull;struct node{int to, val;node(int a, int b) { to = a; val = b; }};vector<node> mp[N];int dis[N], city[N], pa[N], path[N];bool vis[N];bool mark[N];void spfa(int sta, int n){memset(vis, 0, sizeof(vis));fill(dis, dis + n + 1, -INF);deque<int> q;vis[sta] = 1;dis[sta] = 0;q.push_back(sta);while (!q.empty()){int u = q.front();q.pop_front();vis[u] = 0;int sz = mp[u].size();for (int i = 0; i < sz; i++){int v = mp[u][i].to;int w = mp[u][i].val;if (dis[v] < dis[u] + w){dis[v] = dis[u] + w;pa[v] = u;if (!vis[v]){vis[v] = 1;if (!q.empty() && dis[v] >= dis[q.front()])q.push_front(v);elseq.push_back(v);}}}}}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifint t, kase = 0;scanf("%d", &t);while (t--){if (kase)puts("");int n, m;scanf("%d", &n);for (int i = 1; i <= n + 1; i++){mp[i].clear();mark[i] = 0;path[i] = 0;}memset(pa, 0, sizeof(pa));for (int i = 1; i <= n; i++)scanf("%d", &city[i]);scanf("%d", &m);while (m--){int a, b;scanf("%d%d", &a, &b);if (b == n + 1){mark[a] = 1;continue;}mp[a].push_back(node(b, city[b]));}spfa(1, n);int pos = 1, ans = 0;for (int i = 2; i <= n; i++)if (mark[i] && ans < dis[i]){ans = dis[i];pos = i;}int cnt = 0;path[cnt++] = pos;for (int pre = pa[pos]; pre > 1; pre = pa[pre])path[cnt++] = pre;printf("CASE %d#\n", ++kase);printf("points : %d\ncircuit : 1", ans);for (int i = cnt - 1; i >= 0; i--)printf("->%d", path[i]);puts("->1");}#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}

0 0
原创粉丝点击