UVaLive 4256 Salesmen (简单DP)
题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻。
析:dp[i][j] 表示第 i 个数改成 j 时满足条件。然后就很容易了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn<<1][maxn];
bool G[maxn][maxn];
int a[maxn<<1]; int main(){
int T; cin >> T;
while(T--){
int t;
scanf("%d %d", &n, &m);
memset(G, false, sizeof G);
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
G[u][v] = G[v][u] = true;
}
scanf("%d", &t);
for(int i = 0; i < t; ++i) scanf("%d", a+i);
memset(dp, INF, sizeof dp);
for(int i = 1; i <= n; ++i) dp[0][i] = a[0] == i ? 0 : 1;
for(int i = 1; i < t; ++i)
for(int j = 1; j <= n; ++j){
dp[i][j] = a[i] == j ? dp[i-1][j] : dp[i-1][j] + 1;
for(int k = 1; k <= n; ++k)
if(G[j][k]) dp[i][j] = min(dp[i][j], a[i] == j ? dp[i-1][k] : dp[i-1][k] + 1);
} int ans = INF;
for(int i = 1; i <= n; ++i) ans = min(ans, dp[t-1][i]);
printf("%d\n", ans);
}
return 0;
}
UVaLive 4256 Salesmen (简单DP)的更多相关文章
- UVALIVE 4256 Salesmen
Salesmen Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...
- UVaLive 3530 Martian Mining (简单DP)
题意:给定一个n*m的网格,每个格子里有A矿和B矿数量,A必须由右向左运,B只能从下向上运,中间不能间断,问最大总数量. 析:一个简单DP,dp[i][j] 表示 从 (0, 0) 到 (i, j) ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
随机推荐
- nagios-plugins安装报错--with-mysql: no
--with-mysql: no 解决方法 yum安装mysql-devel yum install mysql-devel
- maven官网下载安装步骤
第一大步:下载. a.俗话说:“巧妇难为无米之炊”嘛!我这里用的是 ZIP Archive 版的,win7 64位的机器支持这个,所以我建议都用这个.因为这个简单嘛,而且还干净. 地址见图 拉倒最下面 ...
- iOS MVVM+RAC 从基础到demo
一.关于经典模式MVC的简介 MVC是构建iOS App的标准模式,是苹果推荐的一个用来组织代码的权威范式,市面上大部分App都是这样构建的,具体组建模式不细说,iOS入门者都比较了解(虽然不一定能完 ...
- P4474 王者之剑
P4474 王者之剑 题目大意 n*m的带权网格,任意选择起点开始时刻为0秒.以下操作,每秒按顺序执行 在第i秒开始的时候,在方格(x,y)上,获得(x,y)的值 在偶数秒,周围四格的值清零 每秒可选 ...
- NMEA码详解【转】
本文转载自:http://m.2cto.com/kf/201610/556695.html GPS数据遵循NMEA-0183协议,该数据标准是由NMEA(National Marine Electro ...
- POJ1743 Musical Theme —— 后缀数组 重复出现且不重叠的最长子串
题目链接:https://vjudge.net/problem/POJ-1743 Musical Theme Time Limit: 1000MS Memory Limit: 30000K Tot ...
- 虚拟化技术及ip netns简介
虚拟化技术: Iass:infrastructure as a server 直接启动一个虚拟机,需要什么程序自己安装 Paas:platform as a servicce 启动一个虚拟机,并安装了 ...
- (2)struts2配置祥解
struts工作流程 反射 : 1.构造对象使用构造器 //类似为Servlet public class AddAction { public AddAction(){ System.out.pri ...
- 单页导航菜单视觉设计HTML模板
单页导航菜单视觉设计HTML模板,视觉,企业,html,单页,单页导航菜单视觉设计HTML模板是一款磨砂背景的大气时尚HTML设计网页模板 http://www.huiyi8.com/moban/
- Linux_异常_01_CentOS7无法ping 百度
一.原因 vi /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no B ...