程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1224 Free DIY Tour

HDU 1224 Free DIY Tour

編輯:C++入門知識

題目:
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1894    Accepted Submission(s): 619


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

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
 

 


分析與總結:
有點像TSP問題,但是這題已經告訴我們會把最終回來到1的那點看成是n+1點。所以直接當作是求1~n+1的最長路即可。這題還要保存路徑。
用單源最短路算法或者用我剛剛學到的Floyd記錄路徑方法均可。
輸出時需要注意,當輸出n+1時要把它變成輸出1.


代碼:

1.  SPFA
[cpp]
#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include<queue> 
using namespace std; 
 
const int INF = 0xfffffff; 
const int VN  = 105; 
 
int n,m; 
int w[VN][VN]; 
int point[VN]; 
int d[VN]; 
int pre[VN]; 
bool inq[VN]; 
int ans[VN]; 
 
void init(){ 
    point[n] = 0; 
    for(int i=0; i<=n; ++i){ 
        w[i][i] = INF; 
        for(int j=i+1; j<=n; ++j) 
            w[i][j]=w[j][i]=INF; 
    } 

 
void SPFA(int src){ 
    memset(inq, 0, sizeof(inq)); 
    memset(pre, -1, sizeof(pre)); 
    for(int i=1; i<=n; ++i) d[i]=-INF; 
    d[src] = 0; 
    queue<int>q; 
    q.push(src); 
    while(!q.empty()){ 
        int u = q.front(); q.pop(); 
        inq[u] = false; 
        for(int v=1; v<=n; ++v)if(w[u][v]!=INF){ 
            int tmp = d[u] + w[u][v]; 
            if(d[v] < tmp){ 
                d[v] = tmp; 
                pre[v] = u; 
                if(!inq[v]){ 
                    inq[v] = true; 
                    q.push(v); 
                } 
            } 
        } 
    } 

 
void print_path(int u){ 
    if(pre[u]==-1){ 
        printf("1"); 
        return; 
    } 
    print_path(pre[u]); 
    if(u==n) printf("->%d",1); 
    else printf("->%d",u); 

 
int main(){ 
    int T,u,v,cas=1; 
    scanf("%d",&T); 
    while(T--){ 
        scanf("%d",&n); 
        ++n; 
        init(); 
        for(int i=1; i<n; ++i) 
            scanf("%d",&point[i]);  
        scanf("%d",&m);  
        for(int i=0; i<m; ++i){ 
            scanf("%d%d",&u,&v); 
            w[u][v] = point[v]; 
        } 
        SPFA(1); 
 
        if(cas!=1)puts(""); 
        printf("CASE %d#\n", cas++); 
        printf("points : %d\n", d[n]); 
        printf("circuit : "); 
        print_path(n); 
        puts(""); 
    } 
    return 0; 

 

2. Floyd
[cpp]
#include<iostream> 
#include<cstdio> 
#include<cstring> 
#include<queue> 
using namespace std; 
 
const int INF = -0xfffffff; 
const int VN  = 105; 
 
int n,m; 
int w[VN][VN]; 
int path[VN][VN]; 
int point[VN]; 
int d[VN]; 
bool inq[VN]; 
int ans[VN]; 
 
void init(){ 
    point[n] = 0; 
    for(int i=0; i<=n; ++i){ 
        for(int j=1; j<=n; ++j){ 
            if(i==j) w[i][j]=0; 
            else w[i][j]=INF; 
            path[i][j] = j; 
        } 
    } 

 
void Floyd(){ 
    for(int k=1; k<=n; ++k) 
    for(int i=1; i<=n; ++i) 
    for(int j=1; j<=n; ++j)if(w[i][k]!=INF && w[k][j]!=INF){ 
        int tmp=w[i][k]+w[k][j]; 
        if(w[i][j] < tmp){ 
            w[i][j] = tmp; 
            path[i][j] = path[i][k]; 
        } 
    } 

int main(){ 
    int T,u,v,cas=1; 
    scanf("%d",&T); 
    while(T--){ 
        scanf("%d",&n); 
        ++n; 
        init(); 
        for(int i=1; i<n; ++i) 
            scanf("%d",&point[i]);  
        scanf("%d",&m);  
        for(int i=0; i<m; ++i){ 
            scanf("%d%d",&u,&v); 
            w[u][v] = point[v]; 
        } 
        Floyd(); 
        if(cas!=1)puts(""); 
        printf("CASE %d#\n", cas++); 
        printf("points : %d\n", w[1][n]); 
        printf("circuit : 1"); 
        int u = 1; 
        while(u!=n){ 
            printf("->%d", path[u][n]==n?1:path[u][n]); 
            u=path[u][n]; 
        } 
        puts(""); 
    } 
    return 0; 


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved