Salesmen

Time Limit: 3000ms
Memory Limit: 131072KB

This problem will be judged on UVALive. Original ID: 4256
64-bit integer IO format: %lld      Java class name: Main

 

Traveling salesmen of nhn. (the prestigious Korean internet company) report their current location to the company on a regular basis. They also have to report their new location to the company if they are moving to another location. The company keep each salesman's working path on a map of his working area and uses this path information for the planning of the next work of the salesman. The map of a salesman's working area is represented as a connected and undirected graph, where vertices represent the possible locations of the salesman an edges correspond to the possible movements between locations. Therefore the salesman's working path can be denoted by a sequence of vertices in the graph. Since each salesman reports his position regularly an he can stay at some place for a very long time, the same vertices of the graph can appear consecutively in his working path. Let a salesman's working path be correct if two consecutive vertices correspond either the same vertex or two adjacent vertices in the graph.

For example on the following graph representing the working area of a salesman,

a reported working path [1 2 2 6 5 5 5 7 4] is a correct path. But a reported working path [1 2 2 7 5 5 5 7 4] is not a correct path since there is no edge in the graph between vertices 2 a 7. If we assume that the salesman reports his location every time when he has to report his location (but possibly incorrectly), then the correct path could be [1 2 2 4 5 5 5 7 4], [1 2 4 7 5 5 5 7 4], or [1 2 2 6 5 5 5 7 4].

The length of a working path is the number of vertices in the path. We define the distance between two paths A = a1a2...an <tex2html_verbatim_mark>and B = b1b2...bn<tex2html_verbatim_mark>of the same length n <tex2html_verbatim_mark>as

dist(AB) = d (ai, bi)

where

d (ab) = 

Given a graph representing the working area of a salesman and a working path (possible not a correct path), A <tex2html_verbatim_mark>, of a salesman, write a program to compute a correct working path, B <tex2html_verbatim_mark>, of the same length where the distance dist(AB) <tex2html_verbatim_mark>is minimized.

 

Input

The program is to read the input from standard input. The input consists of T <tex2html_verbatim_mark>test cases. The number of test cases (T) <tex2html_verbatim_mark>is given in the first line of the input. The first line of each test case contains two integers n1 <tex2html_verbatim_mark>, n2 <tex2html_verbatim_mark>(3n1100, 2n24, 950) <tex2html_verbatim_mark>where n1 <tex2html_verbatim_mark>is the number of vertices of the graph representing the working map of a salesman and n2 <tex2html_verbatim_mark>is the number of edges in the graph. The input graph is a connected graph. Each vertex of the graph is numbered from 1 to n1 <tex2html_verbatim_mark>. In the following n2 <tex2html_verbatim_mark>lines, each line contains a pair of vertices which represent an edge of the graph. The last line of each test case contains information on a working path of the salesman. The first integer n <tex2html_verbatim_mark>(2n200) <tex2html_verbatim_mark>in the line is the length of the path and the following n integers represent the sequence of vertices in the working path.

 

Output

Your program is to write to standard output. Print one line for each test case. The line should contain the minimum distance of the input path to a correct path of the same length.

 

Sample Input

2
7 9
1 2
2 3
2 4
2 6
3 4
4 5
5 6
7 4
7 5
9 1 2 2 7 5 5 5 7 4
7 9
1 2
2 3
2 4
2 6
3 4
4 5
5 6
7 4
7 5
9 1 2 2 6 5 5 5 7 4

Sample Output

1
0

Source

 
解题:动态规划,dp[i][j]表示起始位置到当前位置j需要改变的最小值,dp[i][j] = min(dp[i][j],dp[i-1][k]+cost)
 
情况分析:如果目的点j正式当前输入的,那么cost为0,因为这个字符跟原来的一样,如果当前的态,与输入的不一样,那么,需要改变一次。
 
好吧,说不清了,其实就是将原来的路径,修改尽可能小的次数,使得路径合法。
 
 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
bool g[maxn][maxn];
int dp[][maxn];
int main() {
int t,n,m,p,u,v;
scanf("%d",&t);
while(t--) {
memset(g,false,sizeof(g));
scanf("%d %d",&n,&m);
for(int i = ; i < m; ++i) {
scanf("%d %d",&u,&v);
g[u][v] = g[v][u] = true;
}
for(int i = ; i <= n; ++i) dp[][i] = ;
scanf("%d %d",&p,&u);
dp[][u] = ;
for(int i = ; i < p; ++i) {
scanf("%d",&u);
for(int j = ; j <= n; ++j) {
dp[i][j] = INF;
int cost = u == j?:;
for(int k = ; k <= n; ++k) {
if(g[k][j] || k == j)
dp[i][j] = min(dp[i][j],dp[i-][k]+cost);
}
} }
int ans = INF;
for(int i = ; i <= n; ++i)
ans = min(ans,dp[p-][i]);
cout<<ans<<endl;
}
return ;
}

UVALIVE 4256 Salesmen的更多相关文章

  1. UVaLive 4256 Salesmen (简单DP)

    题意:给一个无向连通图,和一个序列,修改尽量少的数,使得相邻两个数要么相等,要么相邻. 析:dp[i][j] 表示第 i 个数改成 j 时满足条件.然后就很容易了. 代码如下: #pragma com ...

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

  3. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  4. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  5. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  6. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  7. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  8. UVA 1424 二 Salesmen

    Salesmen Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  9. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. 告诉你 SQL 数据库与 NoSQL 数据库的区别

    简单来说 SQL 数据库和 NoSQL 数据库有着共同的目标:存储数据,但存储的方式不同 一. 表 SQL中的表结构具有严格的数据模式约束: 存储数据很难出错. NoSQL存储数据更加灵活自由:可能导 ...

  2. PatentTips - Uncore thermal management

    BACKGROUND The field of invention relates to the computer sciences, generally, and, more specificall ...

  3. Servlet过滤器和监听器知识总结

    Servlet过滤器是 Servlet 程序的一种特殊用法,主要用来完成一些通用的操作,如编码的过滤.判断用户的登录状态.过滤器使得Servlet开发者能够在客户端请求到达 Servlet资源之前被截 ...

  4. rails create方法ActiveModel::ForbiddenAttribute的问题

    rails create方法ActiveModel::ForbiddenAttribute的问题 def create @ad = Ad.new(ad_params) @ad.save end pri ...

  5. (十一)Unity5新特性----实战2D游戏

    孙广东  2015.7.11 在本教程中,将了解到U5新功能.你通过本教程.您将了解下面内容: Changes in Component Access Physics Effectors Adding ...

  6. Spark SQL Catalyst源代码分析之Analyzer

    /** Spark SQL源代码分析系列文章*/ 前面几篇文章解说了Spark SQL的核心运行流程和Spark SQL的Catalyst框架的Sql Parser是如何接受用户输入sql,经过解析生 ...

  7. CentOS 6 安装最新的 Redis 2.8 ,安装 TCMalloc

    1,遇到的问题就是 redis 2.8 版本号依赖 Google 的 TCMalloc TCMalloc(Thread-Caching Malloc)是google开发的开源工具──"goo ...

  8. NAS配置Time Machine,在D-Link DNS-320上的配置笔记

    今天打算把Time Machine备份的工作交给NAS,曾经是放在一块外置硬盘上的,尽管速度要比NAS快,可是每次插拔外接都有些麻烦.而NAS又具有实时在线.定时关机启动的功能.配合Time Mach ...

  9. Gradle之依赖管理

    Gradle之依赖管理 泡在网上的日子 / 文 发表于2015-01-29 16:12 第8824次阅读 Gradle,Android Studio 2 编辑推荐:稀土掘金,这是一个针对技术开发者的一 ...

  10. webservice为什么不能用List参数,而只能用数组代替,我想是否因为List没有具体的类型信息,但用泛型的List(如:List<customer>)为什么也不行。如果用作参数的类中含有List<T>字段该如何处理?webservice参数是否支持

    转自:https://social.microsoft.com/Forums/zh-CN/aded4301-b5f1-4aa6-aa46-16c46a60d05e/webservice20026201 ...