zoj 1203 Swordfish 【最小生成树 kruskal && prim】
2015-08-12 20:08
507 查看
Swordfish
Time Limit: 2 Seconds Memory Limit: 65536 KB
There exists a world within our world
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.
We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best hacker in
the world, to help him break into the password protecting the bank system. Stanley's lovely daughter Holly was seized by Gabriel, so he had to work for him. But at the last moment, Stanley made some little trick in his hacker mission: he injected a trojan
horse in the bank system, so the money would jump from one account to another account every 60 seconds, and would continue jumping in the next 10 years. Only Stanley knew when and where to get the money. If Gabriel killed Stanley, he would never get a single
dollar. Stanley wanted Gabriel to release all these hostages and he would help him to find the money back.
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:
Sample Output:
我去,今天重做才发现,题不难,格式真是特么够了,老PE....
深深的无奈。
不多说,直接上代码;
kruskal:
===================================================================================================
prim:
Time Limit: 2 Seconds Memory Limit: 65536 KB
There exists a world within our world
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.
We all remember that in the movie Swordfish, Gabriel broke into the World Bank Investors Group in West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best hacker in
the world, to help him break into the password protecting the bank system. Stanley's lovely daughter Holly was seized by Gabriel, so he had to work for him. But at the last moment, Stanley made some little trick in his hacker mission: he injected a trojan
horse in the bank system, so the money would jump from one account to another account every 60 seconds, and would continue jumping in the next 10 years. Only Stanley knew when and where to get the money. If Gabriel killed Stanley, he would never get a single
dollar. Stanley wanted Gabriel to release all these hostages and he would help him to find the money back.
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:
5 0 0 0 1 1 1 1 0 0.5 0.5 0
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 0x3f3f3f double 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; }
相关文章推荐
- iOS8、Xcode6下 设置Launch Image 启动图片
- Channel Allocation
- [概率dp] hdu 5378 Leader in Tree Land
- session原理
- 如果父视图半透明, 如何使子试图不是半透明
- 【】产品之间的聊天:产品经理半路出家
- 2015 HUAS Summer Trainning #5~J
- Channel Allocation
- JavaScript 闭包究竟是什么
- 杭电1162Eddy's picture(最小生成树)
- 关于windows系统下eclipse开发NDK,是否还需要cygwin的问题
- 文章标题
- unity调用摄像头
- Linux下菜单编辑配置
- cocos2dx基础篇——音乐音效SimpleAudioEngine
- Process 后, 如何获取打开窗口的句柄(根据进程句柄获取窗口句柄)
- poj2442---Sequence
- PDO对象之query()方法
- poj1325最小顶点覆盖
- html 学习笔记(9)