题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻。

析: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)的更多相关文章

  1. UVALIVE 4256 Salesmen

    Salesmen Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...

  2. UVaLive 3530 Martian Mining (简单DP)

    题意:给定一个n*m的网格,每个格子里有A矿和B矿数量,A必须由右向左运,B只能从下向上运,中间不能间断,问最大总数量. 析:一个简单DP,dp[i][j] 表示 从 (0, 0) 到 (i, j) ...

  3. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

  5. 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 ...

  6. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  7. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  8. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  9. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

随机推荐

  1. 兼容性强、简单、成熟、稳定的RTMPClient客户端拉流功能组件EasyRTMPClient

    EasyRTMPClient EasyRTMPClient拉流功能组件是EasyDarwin流媒体团队开发.提供和维护的一套非常稳定.易用.支持重连的RTMPClient工具,SDK形式提供,全平台支 ...

  2. pip3 Fatal error in launcher: Unable to create process using '"' [转]

    在新环境上安装python的时候又再次遇到了这个情况,这次留意了一下,发现原来的文章有错误的地方,所以来更新一下,应该能解决大部分的问题. 环境是win8,原来只安装了python2.7.后来因为要用 ...

  3. Git with SVN

    1)GIT是分布式的,SVN不是: 这 是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别.好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并, ...

  4. java 核心技术卷一 知识点

    第九章 集合 1.Iterator和Iterable接口类,作用. 2.Collection接口类,作用. 3.Map接口类,作用.

  5. Cocos2d-X开发中国象棋《九》走棋规则

    在上一节中实现了走棋,这篇博客将介绍中国象棋中的走棋规则 在写博客前先可能一下象棋的走棋规则: 1)将 将的坐标关系:横坐标相等,纵坐标相减绝对值等于1,或者纵坐标相等,横坐标相减绝对值等于1 将的特 ...

  6. 使用 Nginx 提升网站访问速度(转)

    Nginx 简介 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Ig ...

  7. ABAP 调用webservice 错误

    错误:1.soamanager 配置端口错误: 调整端口后报错: java端回复: 嗯 有问题了我待会儿看看应该是数据有问题 

  8. jQuery的事件绑定和解除

    1 . 绑定事件 语法 : bind(type,data,fn) 描述 : 为每一个匹配的特定元素(像 click)绑定一个事件处理器函数. type(String) : 事件类型 data(Obje ...

  9. Kattis - cardhand Card Hand Sorting 【暴力枚举】

    题意 给出 一个扑克牌的序列 求排成一个"有序"序列 最少的插入次数 有序是这样定义的 同一个花色的 必须放在一起 同一花色中的牌 必须是 升序 或者是 降序 然后 A 是最大的 ...

  10. Automator 实例:使用快捷键 实现 快速在当前路径 打开 iTerm

    1. 在 finder -> 应用程序 或 通过 Spotlight 打开:Automator.app 2. 选择新建 “服务” 3. 设置服务,见下图,设置完成之后,command + s 保 ...