九度oj1430题目链接: http://ac.jobdu.com/problem.php?pid=1430;
题意是走完所有用的边,回到起点,起点可以是任意的,求最小的距离。
分析: 初看感觉像是欧拉路的问题。如果是回路呢?只要把最后那边去掉不就是了。
无向图的欧拉回路的满足条件是所有点的度都为偶数。
如果图中所有点的度都是偶数,那不是很好办(全部边权加起来就是结果);
如果图中有点的度数为奇数呢 ?那就把它变为偶数啊,就相当于在原理图上加边。
奇数度的点个数只能是偶数,那么每次都加一条边,边的端点是奇数度点;
dp[s] = min(dp[s], dp[s^(1<<i)^(1<<j)]+ dist[i][j])
然后枚举起点和终点就ok了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll INF = ~0u>>1;
const int maxn = 20;
ll dist[maxn][maxn];
ll dp[1<<maxn];
int d[maxn], n, m;
template <class T>
T min( T &a, T &b ) {
return a> b? b: a;
};
int main( int argc, char **argv ) {
int T; scanf ("%d", &T);
while ( T-- ) {
scanf ("%d%d", &n, &m);
for ( int i=0; i<n; ++i )
for ( int j=i; j<n; ++j )
dist[i][j] = dist[j][i ] = INF;
memset (d, 0, sizeof d);
ll ans = 0;
for ( int a, b, i=0; i<m; ++i ){
ll c;
scanf ("%d%d%lld", &a, &b, &c);
d[--a]++, d[--b]++;
ans += c;
if(dist[a][b] > c)
dist[a][b] = dist[b][a] = c;
}
for ( int k=0; k<n; ++k )
for ( int i=0; i<n; ++i ) if(dist[i][k]^INF)
for ( int j=0; j<n; ++j ) if(dist[k][j]^INF)
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j] );
bool ff=1;
for ( int i=0; i<n && ff; ++i )
for ( int j=i+1; j<n && ff; ++j )
if ( dist[i][j] == INF ) ff=0;
if( !ff ) { puts("-1"); continue; }
int S=0;
for ( int i=0; i<n; ++i )
if( d[i]&1 ) S ^= (1<<i);
int M = 1<<n;
for ( int i=1; i<=S; ++i ) dp[i] = INF;
dp[0] = 0;
for ( int s=1; s<S; ++s ) {
int i, j;
for ( i=0; i<n && !(s&(1<<i) ); ++i );
for ( j=i+1; j<n; ++j ) if(s&(1<<j)) {
int news = s^(1<<i)^(1<<j);
if(dp[news] == INF ) continue;
dp[s] = min(dp[s], dp[news]+dist[i][j] );
}
}
ll tmp = dp[S];
for ( int i=0; i<n; ++i ) if(S&(1<<i) )
for ( int j=i+1; j<n; ++j ) if(S&(1<<j) ) {
int news = S^(1<<i)^(1<<j);
if(dp[news] < tmp ) tmp = dp[news];
}
printf("%lld\n", ans+tmp);
}
};
codeforces 21 D Traveling Graph 题目链接: http://www.codeforces.com/contest/21/problem/D
这题和上面这题几乎是一样的。
题意是从1出发走完所有边回到1点,求最小的代价。
分析和上面是一样的,直接就是欧拉路问题;
转移方程是一样的;直接上代码;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef __int64 ll;
const int maxn = 16;
int n, m;
ll dp[1<<maxn];
bool vis[maxn];
ll dist[maxn][maxn];
int d[maxn];
#define INF 1<<30
int main( ) {
while( cin >> n >> m ) {
if(m == 0) {
puts("0"); return 0;
}
ll ret = 0;
for ( int i=1; i<=n; ++i ) d[i] = 0;
for ( int i=1; i<=n; dist[i][i]=0, ++i )
for ( int j=i+1; j<=n; ++j )
dist[i][j] = dist[j][i] = INF;
for ( int i=0, a, b, c; i<m; ++i ) {
scanf ("%d%d%d", &a, &b, &c);
ret += c;
vis[a] = vis[b] = true;
if(dist[a][b] > c)
dist[a][b] = dist[b][a] = c;
d[a]++, d[b]++;
}
for ( int k=1; k<=n; ++k )
{
for ( int i=1; i<=n; ++i ) if(dist[i][k]^INF )
for ( int j=1; j<=n; ++j ) if(dist[k][j]^INF )
if(dist[i][j] > dist[i][k] + dist[k][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
bool ff = 1;
for ( int i=1; i<=n && ff; ++i )
if(dist[1][i] == INF && vis[i]) ff = 0;
if(ff == 0) {
puts("-1"); continue;
}
int M = 1<<n;
for ( int i=1; i<M; ++i )
dp[i] = INF;
int S = 0;
for ( int i=1; i<=n; ++i )
if(d[i]&1 ) S |= (1<<(i-1));
/// for ( int i=1; i<=n; ++i ) dp[1<<(i-1)] = 0;
//cout<<"****" << S << endl;
for ( int s=0; s<=S; ++s )
{
int i, j;
for ( i=n-1;i>=0 && !(s&(1<<i)); --i );
for ( j=i-1; j>=0; --j ) if(s&(1<<j) )
{
int news = s^(1<<i)^(1<<j);
if(dp[news] == INF) continue;
int tmp = dp[ news] + dist[i+1][j+1];
if(tmp < dp[s]) dp[s] = tmp;
}
}
cout << ret+dp[S] << endl;
}
};
代码写的有点挫。。。。