HDU1224 Free DIY Tour(spfa+记录路径)

来源:互联网 发布:aion捏脸数据 编辑:程序博客网 时间:2024/05/22 18:01


Link: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): 5097    Accepted Submission(s): 1635


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
杭州电子科技大学第三届程序设计大赛
 

AC code:

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>#include<vector>#include<map>#include<cmath>#define LL long long#define MAXN 1000010using namespace std;const int INF=0x3f3f3f3f;int n,m,tot;int head[MAXN];struct node{int from;int to;int w;int next;}edge[MAXN];int dis[MAXN];bool inq[MAXN];int cnt[MAXN];int pre_edge[MAXN];int path[MAXN];int point[MAXN];int len;void init(){memset(head,-1,sizeof(head));tot=0;}void add(int from,int to,int w){edge[tot].from=from;edge[tot].to=to;edge[tot].w=w;edge[tot].next=head[from];head[from]=tot++;}bool spfa(int st,int ed){deque<int>q;memset(dis,-INF,sizeof(dis));memset(inq,false,sizeof(inq));memset(cnt,0,sizeof(cnt));memset(pre_edge,-1,sizeof(pre_edge));//dis[st]=0;q.push_back(st);inq[st]=true;cnt[st]++;while(!q.empty()){int now=q.front();q.pop_front();inq[now]=false;if(cnt[now]>=n+1){continue;}if(cnt[now]==n+1){dis[now]=INF;}for(int i=head[now];i!=-1;i=edge[i].next){int nex=edge[i].to;if(dis[nex]<dis[now]+point[nex]){dis[nex]=dis[now]+point[nex];pre_edge[nex]=i;if(!inq[nex]){inq[nex]=true;cnt[nex]++;if(!q.empty()&&dis[nex]>dis[q.front()]){q.push_front(nex);}else{q.push_back(nex);}}}}}return true;}void print_path(){int i=pre_edge[n+1];len=0;path[++len]=1;while(i!=-1){int from=edge[i].from;path[++len]=from;i=pre_edge[from];}printf("circuit : 1");for(int j=len-1;j>=1;j--){printf("->%d",path[j]);}printf("\n");}int main(){//freopen("D:\in.txt","r",stdin);int T,cas,i,j,u,v,w;scanf("%d",&T);for(cas=1;cas<=T;cas++){scanf("%d",&n);for(i=1;i<=n;i++){scanf("%d",&point[i]); }point[n+1]=point[1];scanf("%d",&m);init();while(m--){scanf("%d%d",&u,&v);add(min(u,v),max(u,v),0);//a city with higher number has no straight flight to a city with lower number/*if(u==1){u=n+1;add(v,u,0);}else if(v==1){v=n+1;add(u,v,0);}*///没看清题意,题意输入数据中已有n+1的点,所以不用自己把1转为n+1!!! }spfa(1,n+1);printf("CASE %d#\n",cas);printf("points : %d\n",dis[n+1]);print_path();if(cas!=T)printf("\n");}return 0; } 


0 0
原创粉丝点击