【HDU-1224】解题报告(最长路,路径)

原始题目

Free DIY Tour

  • Time Limit: 2000/1000 MS (Java/Others)
  • Memory Limit: 65536/32768 K (Java/Others)
  • Total Submission(s): 8700
  • Accepted Submission(s): 2805

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

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

Sample Output

CASE 1#
points : 90
circuit : 1->3->1

CASE 2#
points : 90
circuit : 1->2->1

Author

JGShining(极光炫影)

Source

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

Recommend

Ignatius.L

题目大意

  • 自定义旅途路线,1和n+1结点权值为0,剩下结点权值给定,有向图,求最长路。

解题思路

  • 改了一下Dijkstra最短路算法竟然A了。

解题代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define np next_permutation
#define ms(x,a) memset((x),(a),sizeof(x))
//优先队列迪杰斯特拉
const int maxn=1e5+5;
struct Edge{
int u,v,w;
Edge(int _u,int _v,int _w):u(_u),v(_v),w(_w){
};
};
vector <Edge> edges[maxn];

int dis[maxn],path[maxn],num[maxn],ans[maxn];
bool vis[maxn];
int n,m,uu,vv,ww,t;

void init(){ //n个结点的边集都清空
rep(i,0,maxn){
edges[i].clear();
}
ms(vis,0);
ms(dis,-INF);
ms(path,0);
ms(num,0);
}

void addEdge(int u,int v,int w){
edges[u].pb(Edge(u,v,w));
}

struct Node{
int Id,W;
Node(int _Id,int _W):Id(_Id),W(_W){
};
bool operator < (const Node &b) const {W<b.W;}
};

void Dijkstra(int s){
priority_queue <Node> q;
q.push(Node(s,0)) ,dis[s]=0;
while(!q.empty()){
Node now=q.top();
q.pop();
int index=now.Id;
if(dis[index]!=now.W) continue;
rep(i,0,edges[index].size()){
// cout<<"dis="<<dis[edges[index][i].v]<<" new cost="<<dis[index]+edges[index][i].w;
if(dis[edges[index][i].v]<dis[index]+edges[index][i].w){
dis[edges[index][i].v]=dis[index]+edges[index][i].w;
q.push(Node(edges[index][i].v,dis[edges[index][i].v]));
path[edges[index][i].v]=index;
// cout<<" #"<< edges[index][i].v<<" #"<<index<<endl;
}
}
}
}

int main(){
// freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
cin>>t;
rep(T,1,t+1){
if(T!=1) cout<<endl;
cin>>n;
init();
rep(i,1,n+1){
cin>>num[i]; //输入点权值
}
cin>>m;
rep(i,1,m+1){
cin>>uu>>vv;
addEdge(uu,vv,num[uu]);
}
Dijkstra(1);
//预处理路径
int cnt=0,ini=n+1;
while(1){
ans[cnt++]=ini;
ini=path[ini];
if(ini==1) break;
}
ans[cnt]=1;
//输出
cout<<"CASE "<<T<<"#"<<endl<<"points : "<<dis[n+1]<<endl;
cout<<"circuit : ";
for(int i=cnt;i>0;i--) cout<<ans[i]<<"->";
cout<<1<<endl;

}
}

收获与反思

  • 需要研究一下Dij可否算最长路