HDU 1224 Free DIY Tour

Source: Internet
Author: User
Tags cas printf time limit

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1224

Topic:

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 are so satisfied and their job that he decide to provide them a free tour around the world. It ' s a good chance to relax themselves.

The tour company shows them a new kind of tour circuit-diy. Each circuit contains some cities which can is selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has it interesting point of, New York has it interesting point, ect. Not any two cities in the world have straight flight the tour, company provide a map to tell it tourists whether they c An got a straight flight between any two 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. This is, they marked the map and one number, a city with higher number has no straight flight Lower number.

Note:weiwei always starts from Hangzhou (in this problem, we assume Hangzhou-is always the "I" and also the last CI Ty, so we mark Hangzhou both 1 andn+1), and it 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 to did you DIY it?

Input
The input would contain several cases. The the ' is ' an integer T which suggests the number of cases. Then T cases follows.
Each case would begin with a 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 a integer m followed by m pairs of integers [Ai, Bi] (1≤i≤m). Each pair of [AI, Bi] indicates that a straight flight are available from city Ai to City Bi.
Output
For each case, your task was to output the maximal summation of interesting points Weiwei and he fellow workers can get th Rough optimal diying and the optimal circuit. The format is as the sample. You may assume that there are only one optimal circuit.

Output a blank line between two cases.

Sample Input

2
3
0,
4
1
2 1 3 2 4 3 4 3 0 4 1 2 1 3 2 4 3 4

Sample Output

Case a
points:90
circuit:1->3->1 case
   
2#
points:90
circuit:1->2->1

Analysis and Summary:
It's a bit like a TSP problem, but this one has already told us that it's going to be the n+1 point when we finally come back to 1. So directly as is to seek 1~n+1 the longest road can be. This question also saves the path.

Using the Single-source shortest path algorithm or using the Floyd method I have just learned is available.

The output needs to be noticed when the output is n+1 to turn it into output 1.

Code:

1. SPFA

#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

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.