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 ...
随机推荐
- python知识点导图(搜集)
第一章 基本环境 第二章 内置类型 第三章 表达式 第四章 函数 第五章 迭代器 第六章 模块 第七章 类 第八章 异常 第九章 装饰器 第十章 描述符 第十一章 元类 第十二章 标准库 Re模块 附 ...
- 解决集群搭建找不到datanode的问题
解决"no datanode to stop"问题当我停止Hadoop时发现如下信息: no datanode to stop原因:每次namenode format会重新创 ...
- MongoDB API java的使用
1.创建一个MongoDB数据库连接对象,它默认连接到当前机器的localhost地址,端口是27017. Mongo mongo=new Mongo(); 2.获得与某个数据库(例如“test”)的 ...
- 九度OJ 1074:对称平方数 (数字特性)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4804 解决:2173 题目描述: 打印所有不超过n(n<256)的,其平方具有对称性质的数. 如11*11=121 输入: 无任何输 ...
- 九度OJ 1046:求最大值 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:9861 解决:4013 题目描述: 输入10个数,要求输出其中的最大值. 输入: 测试数据有多组,每组10个数. 输出: 对于每组输入,请输 ...
- 1000个圆点与PaintDC的使用,OnSize时重画很棒
import wx import random class View(wx.Panel): def __init__(self, parent): super(View, self).__init__ ...
- centos7 安装php
http://blog.csdn.net/zhaozuosui/article/details/48394409
- Java异步编程第2篇
假如如今有一个Buttonbutton,Buttonbutton上有click和doubleclick事件. 两个不同的事件须要进行不同的处理.这时候就须要为对应的事件注冊Listener了.改动后的 ...
- nodejs 基础篇整合
nodeJs 基础篇整合 最近有朋友也想学习nodeJs相关方面的知识,如果你是后端想接近前端,node作为一门跑在服务端的JS语言从这里入门再好不过了.如果你正好喜欢前端,想走的更高,走的更远.no ...
- 经典数学问题<手电过河问题>的动态解法--问题规模扩展至任意大小
非常有趣的一件事是今天在TopCoder的1000分题里面发现了这道经典数学问题. Notes - In an optimal solution ...