题意:在迷宫中有n个点(用字母表示),求一条将所有点都连接起来的最小路径
链接:http://poj.org/problem?id=3026
思路:bfs处理出每条边的长度,建图后最小生成树模板
注意点:无
以下为AC代码:
Run ID | User | Problem | Result | Memory | Time | Language | Code Length | Submit Time |
14177288 | luminous11 | 3026 | Accepted | 784K | 47MS | G++ | 4867B | 2015-05-09 13:55:03 |
#include <algorithm>
#include <iostream>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define RS(s) scanf ( "%s", s )
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define PL() printf ( "\n" )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PSL(s) printf ( "%s\n", s )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define rep(i,m,n) for ( int i = m; i < n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i > n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i < n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i > n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
//#pragma comment ( linker, "/STACK:1024000000,1024000000" )
using namespace std;
const double pi = acos(-1);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
int x, y, cnt;
node(){}
node( int _x, int _y ) : x(_x), y(_y) {}
node( int _x, int _y, int _cnt ) : x(_x), y(_y), cnt(_cnt) {}
};
struct edge{
int to, wei;
edge(){}
edge( int _to, int _wei ) : to(_to), wei(_wei) {}
};
int ma[105][105];
char maps[105][105];
int g[105][105];
bool vis[105][105];
int n, m, cnt;
void bfs ( int x, int y )
{
clr ( vis, 0 );
queue<node> q;
q.push ( node ( x, y, 0 ) );
vis[x][y] = 1;
while ( ! q.empty() ){
node tmp = q.front();q.pop();
if ( ma[tmp.x][tmp.y] != 0 && ma[tmp.x][tmp.y] != -1 ){
g[ma[x][y]][ma[tmp.x][tmp.y]] = g[ma[tmp.x][tmp.y]][ma[x][y]] = min ( g[ma[tmp.x][tmp.y]][ma[x][y]], tmp.cnt );
}
rep ( i, 0, 4 ){
int xi = tmp.x + dir[i][0];
int yi = tmp.y + dir[i][1];
int sp = tmp.cnt + 1;
if ( xi < 0 || xi >= n || yi < 0 || yi >= m || ma[xi][yi] == -1 )continue;
if ( vis[xi][yi] == 0 ){
vis[xi][yi] = 1;
q.push ( node ( xi, yi, sp ) );
}
}
}
}
int prim()
{
int ans = 0;
int dis[105];
int mark[105];
clr ( mark, 0 );
clr ( dis, 0 );
rep ( i, 1, cnt ){
dis[i] = g[1][i];
}
rep ( k, 1, cnt ){
int _min = 0x3f3f3f3f;
int p = -1;
rep ( i, 1, cnt ){
if ( ! mark[i] && _min > dis[i] ){
_min = dis[i];
p = i;
}
}
if ( p == -1 )return -1;
mark[p] = 1;
ans += dis[p];
rep ( i, 1, cnt ){
if ( ! mark[i] && dis[i] > g[p][i] ){
dis[i] = g[p][i];
}
}
}
return ans;
}
void init( int m, int n )
{
clr ( maps, 0 );
clr ( ma, 0 );
clr ( g, 0x3f3f3f3f );
gets( maps[0] );
rep ( i, 0, n ){
gets ( maps[i] );
}
cnt = 1;
rep ( i, 0, n ){
rep ( j, 0, m ){
if ( maps[i][j] == 'S' || maps[i][j] == 'A' ){
ma[i][j] = cnt ++;
}
if ( maps[i][j] == '#' ){
ma[i][j] = -1;
}
}
}
}
int main()
{
int T;
RDI ( T );
while ( T -- ){
RDII ( m, n );
init( m, n );
rep ( i, 0, n ){
rep ( j, 0, m ){
if ( ma[i][j] > 0 ){
bfs ( i, j );
}
}
}
int mst = prim();
PIL ( mst );
}
return 0;
}