HDU 1224 Free DIY Tour

來源:互聯網
上載者:User
Problem DescriptionWeiwei 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 andN+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? 


InputThe 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. 


OutputFor 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
 

利用Dijkstra演算法的思想。依次加入每個點,更新dis[] ,表示interesting
point

#include <stdio.h>#include <iostream>#include <string.h>using namespace std;int t,n,points[110],m;bool map[110][110];int dis[110];int path[110];void printPath(int i){if(i == 0)return;printPath(path[i]);printf("%d->",i);}int main(){//freopen("in.txt", "r", stdin);cin >> t;int c=1;while(t--){cin >> n;for(int i=1; i<=n; i++){cin >> points[i];}points[n+1] = 0;cin >> m;int a,b;memset(map, 0,sizeof(map));memset(dis, 0,sizeof(dis));memset(path, 0,sizeof(path));for(i=1; i<=m; i++){cin >> a >> b;map[a][b] = true;}for(int k=1; k<=n+1; k++){for(i=k+1; i<=n+1; i++){if(map[k][i] ){if(dis[i] < dis[k] + points[i]){dis[i] = dis[k] + points[i];path[i] = k;}}}}printf("CASE %d#\npoints : %d\ncircuit : ",c++,dis[n+1]);printPath(path[n+1]);if(t)printf("1\n\n");elseprintf("1\n");}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.