moving tables
The famous ACM (Advanced Computer Maker) Company has rented a floor of a building whose shape is in the following figure.
The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.
For each room, at most one table will be either moved in or moved out. Now, the manager seeks out a method to minimize the time to move all the tables. Your job is to write a program to solve the manager’s problem.
Input
The input consists of T test cases. The number of test cases ) (T is given in the first line of the input. Each test case begins with a line containing an integer N , 1<=N<=200 , that represents the number of tables to move. Each of the following N lines contains two positive integers s and t, representing that a table is to move from room number s to room number t (each room number appears at most once in the N lines). From the N+3-rd line, the remaining test cases are listed in the same manner as above.
Output
The output should contain the minimum time in minutes to complete the moving, one per line.
Sample Input
3
4
10 20
30 40
50 60
70 80
2
1 3
2 200
3
10 100
20 80
30 50
Sample Output
10
20
30
贪心:路径重叠最多的地方就是至少需要搬的次数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j,T,N,s,t;
int r[200];
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(i=0;i<200;i++)
r[i]=0;
for(i=0;i<N;i++)
{
scanf("%d%d",&s,&t);
if(s>t)
j=s;s=t;t=j;
for(j=(s-1)/2;j<=(t-1)/2;j++)
r[j]++;
}
t=r[0];
for(i=1;i<200;i++)
if(t<r[i]) t=r[i];
printf("%d\n",t*10);
}
return 0;
}
畅通工程
某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?
Input
测试输入包含若干测试用例。每个测试用例的第1行给出两个正整数,分别是城镇数目N ( < 1000 )和道路数目M;随后的M行对应M条道路,每行给出一对正整数,分别是该条道路直接连通的两个城镇的编号。为简单起见,城镇从1到N编号。
注意:两个城市之间可以有多条道路相通,也就是说
3 3
1 2
1 2
2 1
这种输入也是合法的
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最少还需要建设的道路数目。
Sample Input
4 2
1 3
4 3
3 3
1 2
1 3
2 3
5 2
1 2
3 5
999 0
0
Sample Output
1
0
2
998
#include<bits/stdc++.h>
using namespace std;
int n,m,vis[1005];
int find(int x){
return vis[x]==x?x:vis[x]=find(vis[x]);
}
void change(int x, int y){
int c=find(x),d=find(y);
if(c!=d) vis[c]=d;
}
void solve(){
cin>>m;
for(int i=1;i<=n;++i)
vis[i]=i;
while(m--){
int u,v;
cin>>u>>v;
change(u,v);
}
int cnt(0);
for(int i=1;i<=n;++i)
if(vis[i]==i) cnt++;
cout<<cnt-1<<'\n';
}
int main(){
while(cin>>n&&n) solve();
}
486

被折叠的 条评论
为什么被折叠?



