POJ 3164——Command Network——————【最小树形图、固定根】
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 15080 | Accepted: 4331 |
Description
After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.
With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.
Input
The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow Mlines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.
Output
For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy
’.
Sample Input
4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3
Sample Output
31.19
poor snoopy
Source
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Coor{
double x,y;
}coors[maxn];
struct Edge{
int from,to;
double dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
double In[maxn];
double distan(Coor a,Coor b){
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt( dx * dx + dy * dy);
}
double Zhuliu(int root,int n,int m){
double ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = 1.0*INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += In[i];
v = i;
while(vis[v]!= i && ID[v] ==-1 &&v != root){
vis[v] = i;
v = pre[v];
}
if(v != root && ID[v] == -1){
for(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 0; i < n; i++){
scanf("%lf%lf",&coors[i].x,&coors[i].y);
}
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
a--,b--;
edges[i].from = a;
edges[i].to = b;
if(a == b){
edges[i].dist = 1.0*INF;
continue;
}
edges[i].dist = distan(coors[a],coors[b]);
}
double res = Zhuliu(0,n,m);
if(res == -1){
puts("poor snoopy");
}else{
printf("%.2f\n",res);
}
}
return 0;
}
最新文章
- hdu 3632 A Captivating Match(区间dp)
- YHLMR007 服装PDA验货差异报表
- CentOS下判断自己的VPS是OpenVZ的还是Xen的
- Cadence ORCAD CAPTURE元件库介绍
- chrpath工具使用
- CCNA第三讲笔记
- linux下avr单片机开发:中断服务程序
- Java语言基础(五)
- codeforces 100548F (西安现场赛F题):容斥原理
- html加载js那些事
- C#日志工具汇总
- [非官方]ArcGIS10.2 for Desktop扩展工具包——XTools Pro
- ActionBar更改背景颜色(主题)
- 利用Dockerfile构建mysql镜像并实现数据的初始化及权限设置
- CentOS 7 安装.NET Core 2.0
- Python线程模块threading
- server 打开失败
- WinDbg分析Dump常用方法和命令
- Robot Framework浏览器驱动下载
- Jquery 中 ajaxSubmit使用讲解