USACO4.1.3 Fence Loop Non-direction graph minimum ring __usaco

Source: Internet
Author: User

The idea is to ask you to ask for the smallest loop of the graph, but the way to get the data is disgusting.


I use and check the collection + violence of the way ...


First the vertices of each edge are labeled, and then ... The point where a can go to b,b can also go to the side of a, giving and for a point ...


Then Floyd to find the smallest ring.


Floyd the smallest ring I don't quite understand. But use it first and think again in class.



   executing ... Test 1:test OK [0.005 secs, 4028 KB]
   test 2:test OK [0.003 secs, 4028 KB]
   test 3:test OK [0.005 secs, 4028 kb]< C3/>test 4:test OK [0.008 secs, 4028 KB]
   test 5:test OK [0.005 secs, 4028 KB]
   test 6:test OK [0.008 secs, 4028 KB]
   Test 7:test OK [0.014 secs, 4028 KB]
   test 8:test OK [0.049 secs, 4028 KB]
   test 9:test OK [0.041 secs, 4 028 KB] All

tests OK.


/* Task:fence6 lang:c++ * * #include <iostream> #include <cstring> #include <cstdio> #include <cmath&
Gt

#include <vector> using namespace std;
int n;
	struct Edge {int Length;
	int q1_out, q2_out;
	vector<int>q1;
	vector<int>q2;

Edge () {} edge (int _length): Length (_length) {}}x[1000];
int father[1000]= {0};

int g[250][250], dist[250][250];
	int father_is_who (int k) {if (Father[k]) return father[k] = father_is_who (father[k));
else return k;
	} void Init () {scanf ("%d", &n);		
		for (int i = 1; I <= n; + + i) {int Num, Length, OUT1, Out2;
		scanf ("%d%d%d%d", &num, &length, &AMP;OUT1, &out2);
		X[num] = Edge (Length);
			for (int j = 0; J!= Out1; + + j) {int tmp;
			scanf ("%d", &tmp);
		X[num].q1.push_back (TMP);
			for (int j = 0; J!= Out2; + + j) {int tmp;	
			scanf ("%d", &tmp);
		X[num].q2.push_back (TMP); for (int i = 1; I <= n; + + i) {x[i].
		Q1_out = i * 2-1; X[i].
Q2_out = i * 2;	for (int i = 1; I <= n; + i) {for (int j = 0; J!= x[i].q1.size (); + + j) {//i from out1 Point, can go to x[i].q1[j]
			int would = X[i].q1[j];
				for (int k = 0; k!= x[will].q1.size (); + + K)//will departs from OUT1 to will_will {int will_will = x[will].q1[k]; if (Will_will = i) {int a = father_is_who (X[i].	
					Q1_out); int b = father_is_who (X[will].
					Q1_out);
				if (a!= b) father[a] = b;
				for (int k = 0; k!= x[will].q2.size (); + + K)//will departs from OUT1 to will_will {int will_will = x[will].q2[k]; if (Will_will = i) {int a = father_is_who (X[i].	
					Q1_out); int b = father_is_who (X[will].
					Q2_out);
				if (a!= b) father[a] = b;
			for (int j = 0; J!= x[i].q2.size (); + + j) {//i from Out2 point to x[i].q1[j] int will = x[i].q2[j];
				for (int k = 0; k!= x[will].q1.size (); + + K)//will departs from OUT1 to will_will {int will_will = x[will].q1[k]; if (Will_will = i) {int a = father_is_who (X[i].	
					Q2_out); Int b = father_is_who (X[will].
					Q1_out);
				if (a!= b) father[a] = b;
				for (int k = 0; k!= x[will].q2.size (); + + K)//will departs from OUT1 to will_will {int will_will = x[will].q2[k]; if (Will_will = i) {int a = father_is_who (X[i].	
					Q2_out); int b = father_is_who (X[will].
					Q2_out);
				if (a!= b) father[a] = b;
	}} memset (g, sizeof (g)); for (int i = 1; I <= n; + + i) {int a = father_is_who (X[i].
		Q1_out); int b = father_is_who (X[i].	
		Q2_out); G[A][B] = G[b][a] = x[i].
	Length; for (int i = 1; I <= 2 * n; + + i) for (int j = 1; J <= 2 * N; + + j) Dist[i][j] = g[i][j];//cout << g[i][	J] << "";

cout<<endl;
	} void doit () {int ans = 0x7fffffff; for (int k = 1; k <= 2 * n; + + K) {for (int i = 1; I!= K; + i) for (int j = i + 1; j!= K; + + j) if (dist[
		I][J] + g[j][k] + G[k][i] < ans ans = dist[i][j] + g[j][k] + g[k][i]; for (int i = 1; I <= 2 * n; + + i) for (int j = 1; j; = 2 * n;
	+ + j) if (Dist[i][k] + dist[k][j] < Dist[i][j]) dist[i][j] = Dist[i][k] + dist[k][j];
printf ("%d\n", ans);
	int main () {freopen ("fence6.in", "R", stdin);
	Freopen ("Fence6.out", "w", stdout);
	Init ();
	Doit ();
return 0;
 }


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.