UVa1424–Salesmen(DP)
题目大意
给定一个包含n(n<=100)个点的无向连通图和一个长度为L的序列A(L<=200),你的任务是修改尽量少的数,使得序列中的任意两个相邻的数或者相同,或者对应图中两个相邻结点
题解
妈蛋,这么水的题目想不出来,然后搜了下解题报告,瞬间觉得巨水啊。。。。智商拙计啊
用dp[i][j]表示前i个数并且以数字j结尾的序列需要修改的最少次数,那么如果j等于a[i]那么dp[i][j]=min(dp[i-1][k]),否则的话dp[i][j]=min(dp[i-1][k]+1)(j==k或者j和k连通)
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define MAXN 105
#define INF 0x7fffffff
int dp[MAXN*2][MAXN];
bool grah[MAXN][MAXN];
int a[MAXN*2];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n1,n2,n;
scanf("%d%d",&n1,&n2);
memset(grah,false,sizeof(grah));
while(n2--)
{
int x,y;
scanf("%d%d",&x,&y);
grah[x][y]=grah[y][x]=true;
}
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n1;i++)
dp[0][i]=0;
dp[0][a[1]]=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n1;j++)
{
dp[i][j]=INF;
for(int k=1;k<=n1;k++)
if(j==k||grah[j][k])
{
if(j==a[i]) dp[i][j]=min(dp[i][j],dp[i-1][k]) ;
else
dp[i][j]=min(dp[i][j],dp[i-1][k]+1) ;
}
}
int ans=INF;
for(int i=1;i<=n1;i++)
ans=min(ans,dp[n][i]);
printf("%d\n",ans);
}
return 0;
}
UVa1424–Salesmen(DP)的更多相关文章
- 递推DP UVA 1424 Salesmen
题目传送门 /* 题意:给定包含n个点的无向图和一个长度为L的序列,修改尽量少的点使得相邻的数字相同或连通 DP:状态转移方程:dp[i][j] = min (dp[i][j], dp[i-1][k] ...
- UVaLive 4256 Salesmen (简单DP)
题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻. 析:dp[i][j] 表示第 i 个数改成 j 时满足条件.然后就很容易了. 代码如下: #pragma com ...
- 【UVa】Salesmen(dp)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- LA 4256 DP Salesmen
d(i, j)表示使前i个数满足要求,而且第i个数值为j的最小改动次数. d(i, j) = min{ d(i-1, k) | k == j | G[j][k] } #include <cstd ...
- UVA 1424 二 Salesmen
Salesmen Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pr ...
- HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...
- uva1424
Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location t ...
- BIT+DP
2018CCPC网络赛 J - YJJ's Salesman HDU - 6447 YJJ is a salesman who has traveled through western country ...
- UVALIVE 4256 Salesmen
Salesmen Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...
随机推荐
- Educational Codeforces Round 7 F - The Sum of the k-th Powers 拉格朗日插值
The Sum of the k-th Powers There are well-known formulas: , , . Also mathematicians found similar fo ...
- uCGUI窗口操作要点
uCGUI窗口操作要点 1. 创建一个窗口的时候,会给此窗口发送“创建(WM_CREATE)”消息,从而执行它的回调函数:如果创建窗口的标志带有“可视标志(WM_CF_SHOW)”,那么在后续执行GU ...
- 【算法】桶排->冒泡->快排
啊哈 算法 http://pan.baidu.com/s/1jGGl2SI http://pan.baidu.com/s/15C1oq 1 节 最快最简单的排序——桶排序 在我们生活的这个世界中到处都 ...
- Extjs 更新数据集Ext.PagingToolbar的start参数重置的处理
问题:当翻页后,比如当前是第二页,start参数此时是5(初始为0),当切换左侧分类时,我们期望的是从所选分类下明细记录的第一条开始显示,结果发现不是这样,依然是从新数据的第二页开始显示,就是说ext ...
- poj 1348 Computing (四个数的加减乘除四则运算)
http://poj.org/problem?id=1348 Computing Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- Asynchronous socket communication
来源:http://www.codeproject.com/Articles/1608/Asynchronous-socket-communication 本地下载 Download source f ...
- js为链接绑定点击事件并且附带return false;来阻止跳转
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title& ...
- hdu 3480
斜率dp #include<cstdio> #include<cstring> #include<algorithm> #include<queue> ...
- 关于Jquery中ajax方法data参数用法的总结
data 发送到服务器的数据.将自动转换为请求字符串格式.GET 请求中将附加在 URL 后.查看 processData 选项说明以禁止此自动转换.必须为 Key/Value 格式.如果为数组,jQ ...
- Android ListView(Selector 背景图片 全选 Checkbox等按钮)
list_item.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xm ...