Time Limit: 1000MS | Memory Limit: 131072K | |
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 M lines each containing two integers iand 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
朱刘算法大致步骤:
① 找到除根结点的所有点的最小权值入边(非自环)
② 若存在除根结点的其他点无入边(非自环),则无最小树形图,否则继续
③ 若不存在环,则返回当前权值和,否则将每一个环缩成一点
④ 建立新图
先做到简单点最小树形图,看完算法时,还想保留原图继续添加点和边的话,复杂度很高
看了模板后,发现模板直接在原图的点和边操作,简单明了
#include <cstdio> #include <cstring> #include <cmath> using namespace std; const int MAXN=105; const int MAXM=10005; const double INF=1000000007; const double EPS=0.000001; int n,m; int pre[MAXN],id[MAXN],vis[MAXN];//pre[i]表示以i点为终点的最小权值入边的起点,id[i]表示i点在新图上对应的点 double x[MAXN],y[MAXN]; double inEdge[MAXN];//inEdge[i]表示以i点为终点的入边的最小权值 struct Edge { int s,e; double w; }edge[MAXM]; double Directed_MST(int root) { int s,e; double ans=0; while(true) { for(int i=0;i<n;++i) inEdge[i]=INF; for(int i=0;i<m;++i) {//找权值最小的入边 s=edge[i].s; e=edge[i].e; if(edge[i].w<inEdge[e]&&s!=e) {//如果当前边非自环且权值更小 pre[e]=s; inEdge[e]=edge[i].w; } } for(int i=0;i<n;++i)//如果存在一点没有入边则无法形成最小树形图 if(i!=root&&abs(inEdge[i]-INF)<EPS) return -1; int cnt=0;//cnt表示新图的点数 memset(id,-1,sizeof(id)); memset(vis,-1,sizeof(vis)); inEdge[root]=0; for(int i=0;i<n;++i) { ans+=inEdge[i]; int cur=i; while(vis[cur]!=i&&cur!=root&&id[cur]==-1) { vis[cur]=i; cur=pre[cur]; } if(cur!=root&&id[cur]==-1) {//找到一个环 e=pre[cur]; while(e!=cur) {//在一个环上的点都映射到新图上的同一个点 id[e]=cnt; e=pre[e]; } id[cur]=cnt++; } } if(cnt==0)//无环则返回答案 return ans; for(int i=0;i<n;++i)//每个点映射到新图的一个点 if(id[i]==-1) id[i]=cnt++; for(int i=0;i<m;++i) {//建立新图 s=edge[i].s; e=edge[i].e; edge[i].s=id[s]; edge[i].e=id[e]; if(id[s]!=id[e]) edge[i].w-=inEdge[e]; } n=cnt;//更新新图的点数 root=id[root];//更新新图的根 } return ans; } double dist(int s,int e) { return sqrt((x[s]-x[e])*(x[s]-x[e])+(y[s]-y[e])*(y[s]-y[e])); } int main() { while(2==scanf("%d%d",&n,&m)) { for(int i=0;i<n;++i)//点的下标从0开始 scanf("%lf%lf",x+i,y+i); for(int i=0;i<m;++i) {//边的下标从0开始 scanf("%d%d",&edge[i].s,&edge[i].e); --edge[i].s; --edge[i].e; edge[i].w=dist(edge[i].s,edge[i].e); } double ans=Directed_MST(0); if(abs(ans+1)<EPS) printf("poor snoopy\n"); else printf("%.2lf\n",ans); } return 0; }