lightoj1086 Jogging Trails

有个无向网络,现在Robin想从某点出发,经历每条边至少一次,最后回到原点,求最少的权和。

这个有点像是欧拉回路,其实就是的,只是呢,,,有的边会许会重复走。在欧拉回路中,点的度数必然是偶数,这题中的度数为奇数的点的偶数也必然是偶数个,因为这个是无向图。那么,图是连通的,所以最后我们需要将度数为奇数的点进行建边,其实就是这两点之间的最短路上的边再走一次。点只有15个,,,所以状压。dp[sta]表示偶数点的状态为sta的时候的最短路(回路)。从最开始的状态枚举到终态,也就是dp[(1<<n) - 1]。

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const double PI = acos(-1.0);
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dist[20][20];
int deg[20];
int dp[(1<<15)+10];//dp[i]表示偶数定点状态为i时的最短路;
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, n, m;
	int icase = 0;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		memset(dp, INF,sizeof dp);
		memset(dist, INF,sizeof dist);
		memset(deg, 0,sizeof deg);
		int u, v, c;
		int sum = 0, sta = 0;
		for (int i = 1;i <= m;++i){
			scanf("%d%d%d",&u,&v,&c);
			sum += c;
			deg[u]++,deg[v]++;
			dist[u][v] = dist[v][u] = min(dist[u][v], c);
		}
		for (int i = 1;i <= n;++i){
			dist[i][i] = 0;
			if (deg[i]%2==0){
				sta |= (1<<(i-1));
			}
		}
		for (int k = 1;k <= n;++k){
			for (int i = 1;i <= n;++i){
				for (int j = 1;j <= n;++j){
					dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
		dp[sta] = sum;//初始状态
		for (int i = sta;i < (1<<n)-1;++i){//枚举状态
			for (int j = 0;j < n;++j){
				if (i & (1<<j)) continue;
				//(j+1)节点此时的度数为奇;
				for (int k = 0;k < n;++k){
					if (j==k) continue;
					if (i & (1<<k)) continue;
					int now = (i | (1<<j));
					now |= (1<<k);//(k,j)建边
					dp[now] = min(dp[now],dp[i] + dist[j+1][k+1]);
				}
			}
		}
		printf("Case %d: %d\n", ++icase,dp[(1<<n)-1]);
	}
	return 0;
}

这个跑得更快

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dist[20][20];
int deg[20];
int dp[(1<<15)+10];
int n,m;
int dfs(int sta){
	if (dp[sta] != INF) return dp[sta];
	if (sta == (1<<n)-1) return 0;
	for (int i = 0;i < n;++i){
		if (!(sta & (1<<i))){
			for (int j = i + 1;j < n;++j){
				if (!(sta & (1<<j))){
					int nxt = sta|(1<<i)|(1<<j);
					dp[sta] = min(dp[sta], dfs(nxt) + dist[i+1][j+1]);
				}
			}
		}
	}
	return dp[sta];
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		memset(dp, INF,sizeof dp);
		memset(dist, INF,sizeof dist);
		memset(deg, 0,sizeof deg);
		int sum = 0, sta = 0;
		int u, v, c;
		for (int i = 1;i <= m;++i){
			scanf("%d%d%d",&u,&v,&c);
			sum += c;
			deg[u]++;
			deg[v]++;
			dist[u][v] = dist[v][u] = min(dist[u][v], c);
		}
		for (int i = 1;i <= n;++i){
			dist[i][i] = 0;
			if (deg[i]%2==0){
				sta |= (1<<(i-1));
			}
		}
		for (int k = 1;k <= n;++k){
			for (int i = 1;i <= n;++i){
				for (int j = 1;j <= n;++j)
					dist[i][j] = min(dist[i][j],dist[i][k] + dist[k][j]);
			}
		}
		// dp[sta] = sum;
		sum += dfs(sta);
		printf("Case %d: %d\n", ++icase, sum);
	}
	return 0;
}






你可能感兴趣的:(dp,欧拉回路,lightoj)