lightoj1086 Jogging Trails

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

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

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template 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 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<

这个跑得更快

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template 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 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<



你可能感兴趣的:(-----DP-----)