UVA 10952 Rings ' n ' Ropes

Source: Internet
Author: User

Original question:
I have n tiny rings made of steel. I also has m pieces of rope, all of exactly the same length. The ends of each piece of rope is tied to the different rings.
I am going to take one of the rings, L, into my left hand, and another ring, R into my right
Hand. Then I'll pull the whole structure apart as hard as I can. Some of the ropes would be streched horizontally because of this. Others would hang or bend out of shape. If I want the number of horizontally stretched ropes to be as large as possible, which L and R should I pick? Assume the stretching of ropes in negligible, they all has negligible thickness and is free to slide around the Rin GS that they is tied to. The thickness and radius of each ring is negligible, too.
Input
The first line of input gives the number of cases, N. n test Cases follow. Each one starts with the lines containing N (2≤n≤120) and M (0≤m≤n (n−1)/2). The next m lines would each contain a pair of different rings (integers in the range [0,n−1]). Each pair of rings is connected by at the most one rope.
Output
For each test case, output the line containing ' case #x: ', followed by the largest number of ropes that I can stretch Hori Zontally by picking a pair of rings, L and R.
Sample Input
4
2
1
0 1
3
3
0 1
1 2
2 0
6
6
0 1
0 5
1 3
5 4
3 2
4 2
6
7
0 1
0 5
1 3
1 4
5 4
3 2
4 2
Sample Output
Case #1:1
Case #2:1
Case #3:6
Case #4:7

English:
Give you n a ring, with M no stretch of the same length of rope connected together, now the left hand to pick up one of the rings , the right hand to pick up another ring, to both sides stretched hard, can tighten the number of ropes is how much.

#include <bits/stdc++.h> using namespace std;
int mat[121][121];
int n,m,t;
const int inf=999999; void Floyed () {for (int. k=0;k<n;k++) {for (int. i=0;i<n;i++) {for (Int. J=0;j<n
        ; j + +) Mat[i][j]=min (Mat[i][j],mat[i][k]+mat[k][j]);
    }}} int solve () {int ans=0,res;
                for (int i=0;i<n;i++) {for (int j=i+1;j<n;j++) {if (Mat[i][j]!=inf) {
                vector<int> tmp; for (int k=0;k<n;k++) {if (mat[i][j]==mat[i][k]+mat[k][j]) t
                Mp.push_back (k);
                } res=0;
                    for (int k=0;k<tmp.size (), k++) {for (int p=k+1;p<tmp.size ();p + +)
                        {int A, B;
                        A=TMP[K];
                        B=TMP[P]; if (Mat[a][b]==1&&maT[I][A]!=MAT[I][B]) res++;
            }} Ans=max (Ans,res);
}}} return ans;
    } int main () {int k=1;
    Ios::sync_with_stdio (FALSE);
    cin>>t;
        while (t--) {cin>>n>>m;
                for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (i==j) mat[i][j]=0;
        else Mat[i][j]=inf;
            for (int i=1;i<=m;i++) {int A, B;
            cin>>a>>b;
        Mat[a][b]=mat[b][a]=1;
        } floyed ();
        int Ans=solve ();
    cout<< "Case #" <<k++<< ":" <<ans<<endl;
} return 0;
 }

Answer:

Ashamed to say, when reading the question did not pay attention to the hands of each pick up "a" ring. I think of this problem as the largest matching method of the two-figure solution, the sample did not understand not to say, the last dead and alive have not thought out, to see someone else's Chinese explanation for a long time to understand the description of the problem is how to go.

The two hands each with a ring, stretched hard, then the tension of the rope must be two rings between the shortest. First, the floyed algorithm is used to calculate the shortest circuit of all rings, and then to find out all the nodes in the shortest path are recorded.

If the rope is taut, the "fishnet" structure, which is connected by a ring and a rope, must be stretched into a straight line with a hanging ring in the middle. When enumerating the paths of the intermediate nodes, if the distance from the starting point to the two intermediate points is the same, then the two points will be lowered again. So mat[i][a]!=mat[i][b] is the path that satisfies the condition

Find a well-described blog

http://blog.csdn.net/l123012013048/article/details/41910301

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.