题目链接:uva 1424 - Salesmen
题目大意:给定一个包含n个点的无向图和一个长度为L的序列A,要求修改尽量少得数,使得序列中的任意两个相邻数相等或者在途中相邻。
解题思路:dp[i][j]表示说长度为i且最后一个数为j的情况需要修改最少几次。dp[i][j] = min(dp[i][j], dp[i-1][k] + (j == A[i])?1:0));
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;
int n, m, k, g[N][N], A[2*N], dp[N*2][N];
void init () {
int a, b;
memset(g, 0, sizeof(g));
memset(dp, INF, sizeof(dp));
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) dp[0][i] = 0;
for (int i = 0; i < m; i++) {
scanf("%d%d", &a, &b);
g[a][b] = g[b][a] = 1;
}
scanf("%d", &k);
for (int i = 1; i <= k; i++) scanf("%d", &A[i]);
}
int solve() {
int ans = k;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
for (int t = 1; t <= n; t++) if (j == t || g[j][t]) {
dp[i][j] = min(dp[i][j], dp[i-1][t]+(j == A[i] ? 0 : 1));
}
}
}
for (int i = 1; i <= n; i++) {
ans = min(dp[k][i], ans);
}
return ans;
}
int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
printf("%d\n", solve());
}
return 0;
}
分享到:
相关推荐
MTSP_GA Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA) Finds a (near) optimal solution to the M-TSP by setting up a GA to search for the shortest route (least distance needed ...
matlab开发-FixedStartOpenMultipleTravelingSalesmenProblemGeneraticalgorithm。用遗传算法求解具有固定起始点的m-tsp的“开放”变量的近似最优解
通过设置 GA 来搜索最短路线(所需的最短距离或推销员前往每个... 输入: * XY (float) 是一个 Nx2 的城市位置矩阵,其中 N 是城市的数量* max_salesmen (scalar integer) 是最大推销员人数* depots (float) ia 是销
解决多旅行上问题,该 问题与TSP问题不同,TSP问题是围绕用户走一个闭合回路,要求距离最短,而多旅行商问题寻找多个回路,并且距离最短
% MTSPOF_GA Fixed Open Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA) % Finds a (near) optimal solution to a variation of the "open" M-TSP by % setting up a GA to search for the ...
MTSP_GA_MULTI_CH 多重旅行推销员问题 (M-TSP) 使用多染色体表示的遗传算法 (GA) 通过设置找到 M-TSP 变体的(接近)最优解up a GA 搜索最短路径,考虑到额外的限制,并尽量减少销售人员的数量。...
MTSP_GA Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA)
推销员使用Seam Framework for JBoss AS拍卖Web应用程序从原始存储库导入,为 我们是一个由七个学生组成的小组,他们被分配来设计和实施在线拍卖网站(例如eBay)背后的软件。 我们代号为项目销售员。...
Document about traveling salesmen problem
MTSP_GA Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA) Finds a (near) optimal solution to the M-TSP by setting up a GA to search for the shortest route (least distance needed for...
MTSP_GA Multiple Traveling Salesmen Problem (M-TSP) Genetic Algorithm (GA) Finds a (near) optimal solution to the M-TSP by setting up a GA to search for the shortest route (least distance needed for ...
Registers of a corporation with salesmen
During summer vacation,this figure will increase to 72 percent.College students are working as tutors,waiters or salesmen. Why do they want part-time jobs? First,they want to earn money to help ...
经过计算机验证,很好用。 function varargout = mtsp_ga(xy,dmat,salesmen,min_tour,pop_size,num_iter,show_prog,show_res)