light oj 1029 - Civil and Evil Engineer (最大生成树+最小生成树)
2015-10-02 20:31
615 查看
1029 - Civil and Evil Engineer
A Civil Engineer is given a task to connect n houses with the main electric power station directly or indirectly. The Govt has given him permission to connect exactly n wires to connect all of them. Each of the wires connects
either two houses, or a house and the power station. The costs for connecting each of the wires are given.
Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.
Now you are given the task to check whether the Civil Engineer is evil or not. That's why you want to calculate the average before he reports to the Govt.
Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1 to n and the power station is numbered 0.
Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v) meaning that you can connect u and v with a wire and the cost will be w. A line containing
three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than 12000 lines for a case.
relatively-prime. Otherwise print the integer average.
题意:求最大生成树与最小生成树的平均值。
思路:求最大生成树时把边的权值取相反数即可。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 105;
const int MAXM = 200010;
const int N = 1005;
int n;
int mp[MAXN][MAXN],MP[MAXN][MAXN],dist[MAXN];
bool vis[MAXN];
int Prim(int mp[][MAXN])
{
int mi,now,ans=0;
memset(vis,false,sizeof(vis));
memset(dist,INF,sizeof(dist));
for (int i=0;i<=n;i++)
dist[i]=mp[i][0];
dist[0]=0;
vis[0]=true;
for (int i=0;i<n;i++)
{
mi=INF;
now=-1;
for (int j=0;j<=n;j++)
{
if (!vis[j]&&mi>dist[j])
{
now=j;
mi=dist[j];
}
}
if (now==-1) return -1;
vis[now]=true;
ans+=dist[now];
for (int j=0;j<=n;j++)
{
if (!vis[j]&&dist[j]>mp[now][j])
dist[j]=mp[now][j];
}
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,u,v,w,cas=0,t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
memset(mp,INF,sizeof(mp));
memset(MP,INF,sizeof(MP));
while (1)
{
scanf("%d%d%d",&u,&v,&w);
if (u==0&&v==0&&w==0) break;
if (mp[u][v]>w) mp[u][v]=mp[v][u]=w;
if (MP[u][v]>-w) MP[u][v]=MP[v][u]=-w;
}
int ans=Prim(mp)-Prim(MP);
printf("Case %d: ",++cas);
if (ans%2==0) printf("%d\n",ans/2);
else printf("%d/2\n",ans);
}
return 0;
}
![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
either two houses, or a house and the power station. The costs for connecting each of the wires are given.
Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.
Now you are given the task to check whether the Civil Engineer is evil or not. That's why you want to calculate the average before he reports to the Govt.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1 to n and the power station is numbered 0.
Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v) meaning that you can connect u and v with a wire and the cost will be w. A line containing
three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than 12000 lines for a case.
Output
For each case, print the case number and the average as described. If the average is not an integer then print it in p/q form. Where p is the numerator of the result and q is the denominator of the result; p and q arerelatively-prime. Otherwise print the integer average.
Sample Input | Output for Sample Input |
3 1 0 1 10 0 1 20 0 0 0 3 0 1 99 0 2 10 1 2 30 2 3 30 0 0 0 2 0 1 10 0 2 5 0 0 0 | Case 1: 15 Case 2: 229/2 Case 3: 15 |
题意:求最大生成树与最小生成树的平均值。
思路:求最大生成树时把边的权值取相反数即可。
代码:
#include <iostream>
#include <functional>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define DBG pf("Hi\n")
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
#define mod 1000000009
const int maxn = 1005;
const int MAXN = 105;
const int MAXM = 200010;
const int N = 1005;
int n;
int mp[MAXN][MAXN],MP[MAXN][MAXN],dist[MAXN];
bool vis[MAXN];
int Prim(int mp[][MAXN])
{
int mi,now,ans=0;
memset(vis,false,sizeof(vis));
memset(dist,INF,sizeof(dist));
for (int i=0;i<=n;i++)
dist[i]=mp[i][0];
dist[0]=0;
vis[0]=true;
for (int i=0;i<n;i++)
{
mi=INF;
now=-1;
for (int j=0;j<=n;j++)
{
if (!vis[j]&&mi>dist[j])
{
now=j;
mi=dist[j];
}
}
if (now==-1) return -1;
vis[now]=true;
ans+=dist[now];
for (int j=0;j<=n;j++)
{
if (!vis[j]&&dist[j]>mp[now][j])
dist[j]=mp[now][j];
}
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin);
#endif
int i,j,u,v,w,cas=0,t;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
memset(mp,INF,sizeof(mp));
memset(MP,INF,sizeof(MP));
while (1)
{
scanf("%d%d%d",&u,&v,&w);
if (u==0&&v==0&&w==0) break;
if (mp[u][v]>w) mp[u][v]=mp[v][u]=w;
if (MP[u][v]>-w) MP[u][v]=MP[v][u]=-w;
}
int ans=Prim(mp)-Prim(MP);
printf("Case %d: ",++cas);
if (ans%2==0) printf("%d\n",ans/2);
else printf("%d/2\n",ans);
}
return 0;
}
相关文章推荐
- linux下的硬连接和符号链接(软连接)
- 安卓在xml文件(布局文件)中做一些细微改变却运行程序报错的原因
- 推断序列的端部的大小C规划
- 让HTML页面元素居中的各种实现方法
- 网络编程
- SLC教程
- unity3d 中 transform.position 与Rigidbody.position的同步问题
- hdu 5471(状压DP or 容斥)
- Java 常见异常及趣味解释
- 矩阵转置(数据结构课本内容)
- 将博客搬至CSDN
- VTK利用点云高程渲染点云(方法二:vtkUnstructuredGrid)
- iOS8之后UIAlertController
- pat1013Battle Over Cities (25)
- 每日三个笑话-20151002
- ./configure 出现package was not found问题的解决办法
- String
- Java学习笔记----参数传递
- 毛泽东体育文化:文明其精神,野蛮其体魄
- 学习新技术的10个建议