码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3613 Cow Relays

时间:2016-07-12 22:55:58      阅读:232      评论:0      收藏: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 have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two 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 have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

Input

* Line 1: Four space-separated integers: NTS, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

Sample Input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

Source

 

经过n条边的最短路径。

好像可以贪心,先找到一条最短路,然后随便找个最小环绕圈圈,我没有尝试。

 

使用了标准解法,矩阵乘法+倍增floyd

floyd是枚举k点,更新i到j的最短路径,更新后实际上就是从原本的走一条路到达更新成了走两条路到达。

根据这个思想,我们可以倍增跑floyd,就能得出走2^k条边从i到j的最短路径。

 

代码如下:(a[i]里存的就是走2^i条边的最短路邻接矩阵)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 using namespace std;
 7 const int mxn=1000;
 8 bool vis[mxn];
 9 int n;
10 int fa[mxn];
11 int in[mxn],out[mxn];
12 void init(int n){
13     for(int i=1;i<=n;i++)fa[i]=i;
14 }
15 int find(int x){
16     if(fa[x]==x)return x;
17     else return fa[x]=find(fa[x]);
18 }
19 char s[1100];
20 int main(){
21     int T;
22     scanf("%d",&T);
23     while(T--){
24         memset(vis,0,sizeof vis);
25         memset(in,0,sizeof in);
26         memset(out,0,sizeof out);
27         scanf("%d",&n);
28         init(28);
29         int i,j;
30         for(i=1;i<=n;i++){
31             scanf("%s",s);
32             int u=s[0]-a+1;
33             int v=s[strlen(s)-1]-a+1;
34             in[v]++;out[u]++;
35             vis[u]=vis[v]=1;
36             int x=find(u),y=find(v);
37             if(x!=y)fa[y]=x;
38         }
39         int st=0,ed=0;bool flag=0;
40         int cnt=0;
41         for(i=1;i<=26;i++){
42             if(vis[i] && find(i)==i){
43                 cnt++;
44             }
45             if(out[i]!=in[i]){//出度不等于入度时判定 
46                 if(out[i]==in[i]+1){
47                     if(!st)st=i;
48                     else flag=1;
49                 }
50                 else if(in[i]==out[i]+1){
51                     if(!ed)ed=i;
52                     else flag=1;
53                 }
54                 else flag=1;
55             }
56         }
57         if(cnt!=1){//非连通特判 
58             printf("The door cannot be opened.\n");
59             continue;
60             }
61         if(flag==1)printf("The door cannot be opened.\n");
62         else printf("Ordering is possible.\n");
63     }
64     return 0;
65 }

 

 

 

 

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6726   Accepted: 2626

Description

For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two 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 have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

Input

* Line 1: Four space-separated integers: NTS, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

Sample Input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

Source

POJ3613 Cow Relays

标签:

原文地址:http://www.cnblogs.com/SilverNebula/p/5665162.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!