POJ 1751 Highways

来源:互联网 发布:交大医学院网络英语 编辑:程序博客网 时间:2024/04/29 22:38

题目大意:

        只有一个测例,有N个点编号1 ~ N(1 ≤ N ≤ 750),并给出N个点的坐标"X Y",其中0 ≤ X, Y ≤ 10,000,都是整型,接下来会给出M条已经存在的边(0 ≤ M ≤ 1,000),每条边都给出两个端点的编号,问题是还要添加几条边才能使图上所有点都连通并且是新添加的边的总长度最小,只要求输出添加的每条边,每条边包含两个点的编号(用空格隔开),每条边占一行,每条边中点的顺序以及边的输出顺序任意。

题目链接

注释代码:

/*                                     * Problem ID : POJ 1751 Highways  * Author     : Lirx.t.Una                                     * Language   : C++                         * Run Time   : 79 ms                                     * Run Memory : 2880 KB                                    */#include <iostream>#include <cstring>#include <cstdio>#include <queue>//点的最大数量#defineMAXN750using namespace std;structPoint {//点的位置信息intx, y;friend istream &operator>>(istream &is, Point &p) {is >> p.x >> p.y;return is;}intPOW(int a) { return a * a; }intoperator^(Point &oth) {//计算两点距离的平方return POW( x - oth.x ) + POW( y - oth.y );}};structArc {//边的信息,输出答案时用到intu, v;//边的两个端点Arc(int uu, int vv) : u(uu), v(vv) {}friend ostream &operator<<(ostream &os, Arc &arc) {os << arc.u << ' ' << arc.v;return os;}};structNode {//Prim结点intu;intd;Node(void) {}Node(int uu, int dd) : u(uu), d(dd) {}booloperator<(const Node &oth)const {return d > oth.d;}};Pointp[MAXN + 1];intg[MAXN + 1][MAXN + 1];intpre[MAXN + 1];intd[MAXN + 1];boolvis[MAXN + 1];queue<Arc>ans;priority_queue<Node>heap;voidprim(int n) {inti;intnv;intu, v;Nodenode;vis[1] = true;for ( i = 2; i <= n; i++ ) {d[i]   = g[1][i];pre[i] = 1;heap.push(Node( i, d[i] ));}nv = 1;while ( !heap.empty() ) {//输入会出现无法构成生成树的情况!!蛋疼while (true) {node = heap.top();heap.pop();if ( !vis[ u = node.u ] ) {vis[u] = true;nv++;if ( g[ pre[u] ][u] ) ans.push(Arc(pre[u], u));//不是已经造好的路就放入ans队列中break;}}if ( nv == n ) break;for ( v = 2; v <= n; v++ )if ( !vis[v] && g[u][v] < d[v] ) {d[v]   = g[u][v];pre[v] = u;heap.push(Node( v, d[v] ));}}while ( !ans.empty() ) {//将需要新建的路输出cout << ans.front() << endl;ans.pop();}}intmain() {intn, m;//点数,已经修好的路的数量intu, v;//临时点inti, j;scanf("%d", &n);for ( i = 1; i <= n; i++ ) cin >> p[i];for ( i = 1; i <= n; i++ )for ( j = i + 1; j <= n; j++ )g[i][j] = g[j][i] = p[i] ^ p[j];scanf("%d", &m);while ( m-- ) {scanf("%d%d", &u, &v);g[u][v] = g[v][u] = 0;//已经修好的路就不需要耗费}prim(n);return 0;}
无注释代码:

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#defineMAXN750using namespace std;structPoint {intx, y;friend istream &operator>>(istream &is, Point &p) {is >> p.x >> p.y;return is;}intPOW(int a) { return a * a; }intoperator^(Point &oth) {return POW( x - oth.x ) + POW( y - oth.y );}};structArc {intu, v;Arc(int uu, int vv) : u(uu), v(vv) {}friend ostream &operator<<(ostream &os, Arc &arc) {os << arc.u << ' ' << arc.v;return os;}};structNode {intu;intd;Node(void) {}Node(int uu, int dd) : u(uu), d(dd) {}booloperator<(const Node &oth)const {return d > oth.d;}};Pointp[MAXN + 1];intg[MAXN + 1][MAXN + 1];intpre[MAXN + 1];intd[MAXN + 1];boolvis[MAXN + 1];queue<Arc>ans;priority_queue<Node>heap;voidprim(int n) {inti;intnv;intu, v;Nodenode;vis[1] = true;for ( i = 2; i <= n; i++ ) {d[i]   = g[1][i];pre[i] = 1;heap.push(Node( i, d[i] ));}nv = 1;while ( !heap.empty() ) {while (true) {node = heap.top();heap.pop();if ( !vis[ u = node.u ] ) {vis[u] = true;nv++;if ( g[ pre[u] ][u] ) ans.push(Arc(pre[u], u));break;}}if ( nv == n ) break;for ( v = 2; v <= n; v++ )if ( !vis[v] && g[u][v] < d[v] ) {d[v]   = g[u][v];pre[v] = u;heap.push(Node( v, d[v] ));}}while ( !ans.empty() ) {cout << ans.front() << endl;ans.pop();}}intmain() {intn, m;intu, v;inti, j;scanf("%d", &n);for ( i = 1; i <= n; i++ ) cin >> p[i];for ( i = 1; i <= n; i++ )for ( j = i + 1; j <= n; j++ )g[i][j] = g[j][i] = p[i] ^ p[j];scanf("%d", &m);while ( m-- ) {scanf("%d%d", &u, &v);g[u][v] = g[v][u] = 0;}prim(n);return 0;}

0 0