HDU 1224 Free DIY Tour

来源:互联网 发布:程序化交易源码 编辑:程序博客网 时间:2024/05/04 03:49

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1224

Free DIY Tour

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


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?
 

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
给你一个有向图,而且每个顶点也给出了权值,叫你从顶点1出发最终要到达n+1这个点,也就是原点。每个顶点只能经过一次。问你所能经过的点的最大权值是什么。

这个显然不是TSP问题。别搞混淆了。本题可以用DP搞,当然也可以暴搜。

这里是深搜专题,所以就用深搜的方法做。要回溯。比较简单的深搜。

直接上AC的代码。

#include<iostream>#include<vector>using namespace std;const int maxn=105;vector<int>G[maxn];int n,m,v[maxn],Max,p[maxn],ans[maxn];bool vis[maxn];void dfs(int d,int va){    if(d==n+1)    {        if(Max<va)        {            Max=va;            for(int i=0;i<maxn;i++) ans[i]=p[i];        }        return ;    }    for(int i=0;i<G[d].size();i++)    {        int next=G[d][i];        if(!vis[next])        {            vis[next]=1;            p[next]=d;            dfs(next,va+v[next]);            vis[next]=0;        }    }}void print(int d){    if(d==1) cout<<1;    else    {        print(ans[d]);        if(d==n+1) d=1;        cout<<"->"<<d;    }}int main(){    int t,Case=1;    cin>>t;    while(t--)    {        cout<<"CASE "<<Case++<<"#"<<endl;        fill(vis,vis+maxn,0);        for(int i=0;i<maxn;i++) G[i].clear();        cin>>n;        for(int i=1;i<=n;i++) cin>>v[i];        v[n+1]=0;        cin>>m;        for(int i=0;i<m;i++)        {            int x,y;            cin>>x>>y;            G[x].push_back(y);        }        vis[1]=1;        Max=0;        dfs(1,0);        cout<<"points : "<<Max<<endl;        cout<<"circuit : ";        print(n+1);        cout<<endl;        if(t) cout<<endl;    }    return 0;}


0 0
原创粉丝点击