SRM 626 D2L3:NegativeGraphDiv2,构造新图

题目:http://community.topcoder.com/stat?c=problem_statement&pm=13247

参考:http://apps.topcoder.com/wiki/display/tc/SRM+626

图中包含自环和负边,用普通方法很难求解,首先应该构造一个新的图,新图的节点表示为(k, v ),k表示还可以使用k次power,v表示当前节点位置。然后就是求(k, 1 ) 到 (0, N)的最短距离。由于存在负边,所以不能用Dijkstra算法,可以使用Bellman-Ford算法,但这种方法很慢,会超时,需要更快速的方法。使用Bellman-Ford算法的代码如下:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>

#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;

#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair

/*************** Program Begin **********************/
int dist[1001][51];
int INF = 100000001;

class NegativeGraphDiv2 {
public:
	long long findMin(int N, vector <int> s, vector <int> t, vector <int> weight, int charges) {
		long long res = 0LL;
		int E = s.size();

		// Bellman–Ford algorithm
		for (int i = 0; i <= charges; i++) {
			for (int j = 1; j <= N; j++) {
				dist[i][j] = INF;
			}
			dist[i][1] = 0;
		}
		for (int i = 0; i < (charges + 1) * N; i++) {
			for (int j = 0; j < E; j++) {
				int u = s[j], v = t[j], w = weight[j];
				for (int k = 0; k <= charges; k++) {
					if (dist[k][u] + w < dist[k][v]) {
						dist[k][v] = dist[k][u] + w;
					}
					if (k > 0 && dist[k][u] - w < dist[k-1][v]) {
						dist[k-1][v] = dist[k][u] - w;
					}
				}
			}
		}
		res = dist[0][N];
		for (int i = 0; i <= charges; i++) {
			res = min<int>(res, dist[i][N]);
		}

		return res;
	}

};

/************** Program End ************************/


通过观察新构造的图与原图的关系,可以发现一些特殊的性质,详细请看参考。实现代码如下:


代码:

#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>

#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>

#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;

#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
typedef pair<int, int> pii;
typedef long long llong;
typedef pair<llong, llong> pll;
#define mkp make_pair

/*************** Program Begin **********************/
int dist[51][51];
int xdist[1001][51];
int INF = 100000001;

class NegativeGraphDiv2 {
public:
	long long findMin(int N, vector <int> s, vector <int> t, vector <int> weight, int charges) {
		long long res = 0LL;
		int E = s.size();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				dist[i][j] = INF;
			}
			dist[i][i] = 0;
		}

		for (int i = 0; i < E; i++) {
			int u = s[i] - 1, v = t[i] - 1, w = weight[i];
			dist[u][v] = min(dist[u][v], w);
		}
		for (int k = 0; k < N; k++) {
			for (int i = 0; i < N; i++) {
				for (int j = 0; j < N; j++) {
					dist[i][j] = min(dist[i][j], dist[i][k] +dist[k][j]);
				}
			}
		}
		for (int i = 0; i < N; i++) {
			xdist[0][i] = dist[i][N - 1];
		}
		for (int i = 1; i <= charges; i++) {
			for (int j = 0; j < N; j++) {
				xdist[i][j] = xdist[i - 1][j];
				for (int k = 0; k < E; k++) {
					int u = s[k] - 1, v = t[k] - 1, w = -weight[k];
					xdist[i][j] = min(xdist[i][j], dist[j][u] + w + xdist[i - 1][v]);
				}
			}
		}
		res = xdist[charges][0];
		return res;
	}

};

/************** Program End ************************/



// Powered by FileEdit
// Powered by CodeProcessor


1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值