Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6726 |
|
Accepted: 2626 |
Description
For their physical fitness program, n (2≤ n ≤1,000,000) cows has decided to run a relay race using th E t (2≤ t ≤100) cow trails throughout the pasture.
Each trail Connects II different intersections (1≤ i1i ≤ 1,000; 1≤ i2i ≤1,000), EA Ch of which is the termination for at least and trails. The cows know the Lengthi of each trail (1≤ lengthi ≤1,000), the intersections of the trail Conn ECTS, and they know that no, intersections is directly connected by the different trails. The trails form a structure known mathematically as a graph.
To run the relay, the N cows position themselves at various intersections (some intersections might has more tha n One cow). They must position themselves properly so this they can hand off the baton Cow-by-cow and end up at the proper finishing P Lace.
Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and T Raverses exactly N cow trails.
Input
* Line 1:four space-separated integers: N, T, S, and E
* Lines 2. T+1:line i+1 describes trail I with three space-separated integers: lengthi , i< /c10>1i , and i2i
Output
* Line 1: A single integer, that's the shortest distance from intersection S to intersection E . Traverses exactly N cow trails.
Sample Input
2 6 6 411 4 64 4 88 4 96 6 82 6 93 8 9
Sample Output
10
Source
Usaco November Gold
The shortest path through the N-side.
Seems to be greedy, first find a shortest, and then casually find a minimum circle, I did not try.
Using the standard solution, matrix multiplication + multiplication Floyd
Floyd is the enumeration K point, Update I to j the shortest path, after the update is actually from the original way to reach the update into the two routes to arrive.
According to this idea, we can multiply run Floyd, we can get the shortest path from I to J of the 2^k Edge.
The code is as follows: (A[i] is the shortest-path adjacency matrix to the 2^i edge)
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <algorithm>6 using namespacestd;7 Const intmxn= +;8 BOOLVIS[MXN];9 intN;Ten intFA[MXN]; One int inch[MXN], out[MXN]; A voidInitintN) { - for(intI=1; i<=n;i++) fa[i]=i; - } the intFindintx) { - if(fa[x]==x)returnx; - Else returnfa[x]=find (Fa[x]); - } + Chars[1100]; - intMain () { + intT; Ascanf"%d",&T); at while(t--){ -memset (Vis,0,sizeofvis); -Memsetinch,0,sizeof inch); -Memset out,0,sizeof out); -scanf"%d",&n); -Init -); in inti,j; - for(i=1; i<=n;i++){ toscanf"%s", s); + intu=s[0]-'a'+1; - intV=s[strlen (s)-1]-'a'+1; the inch[v]++; out[u]++; *vis[u]=vis[v]=1; $ intX=find (u), y=Find (v);Panax Notoginseng if(x!=y) fa[y]=x; - } the intst=0, ed=0;BOOLflag=0; + intCnt=0; A for(i=1; i<= -; i++){ the if(Vis[i] && find (i) = =i) { +cnt++; - } $ if( out[i]!=inch[i]) {//determine when the degree is not equal to the degree of entry $ if( out[i]==inch[i]+1){ - if(!st) st=i; - Elseflag=1; the } - Else if(inch[i]== out[i]+1){Wuyi if(!ed) ed=i; the Elseflag=1; - } Wu Elseflag=1; - } About } $ if(cnt!=1){//Non-connected special award -printf"The door cannot be opened.\n"); - Continue; - } A if(flag==1) printf ("The door cannot be opened.\n"); + Elseprintf"Ordering is possible.\n"); the } - return 0; $}
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 6726 |
|
Accepted: 2626 |
Description
For their physical fitness program, n (2≤ n ≤1,000,000) cows has decided to run a relay race using th E t (2≤ t ≤100) cow trails throughout the pasture.
Each trail Connects II different intersections (1≤ i1i ≤ 1,000; 1≤ i2i ≤1,000), EA Ch of which is the termination for at least and trails. The cows know the Lengthi of each trail (1≤ lengthi ≤1,000), the intersections of the trail Conn ECTS, and they know that no, intersections is directly connected by the different trails. The trails form a structure known mathematically as a graph.
To run the relay, the N cows position themselves at various intersections (some intersections might has more tha n One cow). They must position themselves properly so this they can hand off the baton Cow-by-cow and end up at the proper finishing P Lace.
Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and T Raverses exactly N cow trails.
Input
* Line 1:four space-separated integers: N, T, S, and E
* Lines 2. T+1:line i+1 describes trail I with three space-separated integers: lengthi , i< /c10>1i , and i2i
Output
* Line 1: A single integer, that's the shortest distance from intersection S to intersection E . Traverses exactly N cow trails.
Sample Input
2 6 6 411 4 64 4 88 4 96 6 82 6 93 8 9
Sample Output
10
Source
Usaco November Gold
POJ3613 Cow Relays