zoj 1203 Swordfish 【最小生成树 kruskal && prim】
来源:互联网 发布:儿童学乐器知乎 编辑:程序博客网 时间:2024/06/05 16:37
A world beneath what we call cyberspace.
A world protected by firewalls,
passwords and the most advanced
security systems.
In this world we hide
our deepest secrets,
our most incriminating information,
and of course, a shole lot of money.
This is the world of Swordfish.
You who has watched the movie know that Gabriel at last got the money by threatening to hang Ginger to death. Why not Gabriel go get the money himself? Because these money keep jumping, and these accounts are scattered in different cities. In order to gather up these money Gabriel would need to build money transfering tunnels to connect all these cities. Surely it will be really expensive to construct such a transfering tunnel, so Gabriel wants to find out the minimal total length of the tunnel required to connect all these cites. Now he asks you to write a computer program to find out the minimal length. Since Gabriel will get caught at the end of it anyway, so you can go ahead and write the program without feeling guilty about helping a criminal.
Input:
The input contains several test cases. Each test case begins with a line contains only one integer N (0 <= N <=100), which indicates the number of cities you have to connect. The next N lines each contains two real numbers X and Y(-10000 <= X,Y <= 10000), which are the citie's Cartesian coordinates (to make the problem simple, we can assume that we live in a flat world). The input is terminated by a case with N=0 and you must not print any output for this case.
Output:
You need to help Gabriel calculate the minimal length of tunnel needed to connect all these cites. You can saftly assume that such a tunnel can be built directly from one city to another. For each of the input cases, the output shall consist of two lines: the first line contains "Case #n:", where n is the case number (starting from 1); and the next line contains "The minimal distance is: d", where d is the minimal distance, rounded to 2 decimal places. Output a blank line between two test cases.
Sample Input:
50 00 11 11 00.5 0.50
Sample Output:
Case #1:The minimal distance is: 2.83
我去,今天重做才发现,题不难,格式真是特么够了,老PE....
深深的无奈。
不多说,直接上代码;
kruskal:
#include<cstdio>#include<cstring>#include<cmath>#define mem(a, b) memset(a, (b), sizeof(a))#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Pf(a) printf("The minimal distance is: %.2lf\n", (a))#define INF 0x3f3f3f#include<algorithm>using namespace std;int per[150], n;double x[150], y[150]; void init(){for(int i = 0; i <= n; i++)per[i] = i;}struct edge{int start, end;double val;};edge p[10100];bool operator < (edge a, edge b){return a.val < b.val;}int find(int x){return (x==per[x]) ? x : (per[x] = find(per[x])); }bool join(int x, int y){int fx = find(x);int fy = find(y);if(fx != fy){per[fx] = fy;return true;}return false;}int main(){int cas = 0;while(Si(n)==1, n){init();int i,j,k;for(i = 0; i < n; i++){scanf("%lf%lf", &x[i], &y[i]);}double ans = 0.00;if(cas > 0)printf("\n");printf("Case #%d:\n", ++cas);for(i = 0,k = 0; i < n; i++){for(j = 0; j < n; j++){double d = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));p[k].start = i;p[k].end = j;p[k].val = d;k++;}}sort(p, p+k);for(i = 0; i < k; i++){if(join(p[i].start, p[i].end))ans += p[i].val;}Pf(ans);}return 0;}
===================================================================================================
prim:
#include<cstdio>#include<cstring>#include<cmath>#define mem(a, b) memset(a, (b), sizeof(a))#define Wi(a) while(a--)#define Si(a) scanf("%d", &a)#define Pf(a) printf("The minimal distance is: %.2lf\n", (a))#define INF 0x3f3f3fdouble map[150][150];double val[150];double x[150], y[150];bool vis[150];int n;void prim(){mem(vis, 0);vis[1] = 1;int i, j, k;double minn, ans = 0.0;for(i = 0; i < n; i++){val[i] = map[1][i];}for(i = 1; i < n; i++){k = 1; minn = INF;for(j = 0; j < n; j++){if(!vis[j] && val[j] < minn){minn = val[j];k = j;}}vis[k] = 1;ans += minn;for(j = 0; j < n; j++){if(!vis[j] && val[j] > map[j][k])val[j] = map[j][k];}}Pf(ans);} int main(){int cas = 0;while(Si(n)==1, n){int i, j, k;for(i = 0; i < n; i++)scanf("%lf%lf", &x[i], &y[i]);for(i = 0; i < n; i++){for(j = 0; j < n; j++){map[i][j] = map[j][i] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));}}if(cas > 0)printf("\n");printf("Case #%d:\n", ++cas);prim();}return 0;}
2 0
- zoj 1203 Swordfish 【最小生成树 kruskal && prim】
- ZOJ 1203 Swordfish(最小生成树 kruskal)
- ZOJ 1203 Swordfish (最小生成树 prim算法)
- ZOJ - 1203 Swordfish (最小生成树 Prim)
- ZOJ 1203 Swordfish 最小生成树 Kruscal && Prim
- zoj 1203 swordfish 最小生成树
- ZOJ 1203 Swordfish【最小生成树】
- ZOJ - 1203 Swordfish (非负权值的最小生成树/最短路 - Kruskal算法)
- ZOJ 1203 Swordfish ( prim )
- zoj 1203 Swordfish(Prim!)
- zoj 1203 Swordfish【prim】
- Prim zoj 1203 Swordfish
- zoj 1203 Swordfish(最小生成树))
- zoj 1406 最小生成树 kruskal 和 prim用法
- zoj 1586 QS Network【最小生成树 kruskal && prim】
- ZOJ 2048 Highways 最小生成树 Kruskal && Prim
- zoj 2326 Tangled in Cables【最小生成树 kruskal && prim】
- Prim、Kruskal最小生成树
- Process 后, 如何获取打开窗口的句柄(根据进程句柄获取窗口句柄)
- unity调用摄像头
- 文章标题
- 4种常见食物不宜和猪肉搭配
- dfs+剪枝 poj1011
- zoj 1203 Swordfish 【最小生成树 kruskal && prim】
- CodeForces 25A IQ test 唯一奇偶
- session原理
- React Native 简介:用 JavaScript 搭建 iOS 应用(2)
- iOS8、Xcode6下 设置Launch Image 启动图片
- Channel Allocation
- 君と彼女の恋
- hdu(1078)——FatMouse and Cheese(递推型动归)
- poj 3713 三联通图