2014 Super Training #2 F The Bridges of Kolsberg --DP
原题:UVA 1172 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3613
动态规划问题。
定义: dp[i] = 右岸前i个村庄(m岸)能够与左岸(n岸)不交叉匹配的最大权值和最小桥数 (用pair<int,int> 维护两个值)
方程:
dp[i].first = max(dp[i].first,dp[i-1].first(i>=1)+cost1[i]+cost2[j]) when 左岸的i与右岸的j相匹配
dp[i].second = dp[i-1].second(i>=1)+1 (if 上面dp[i].first更小)
从后往前枚举,然后从前往后更新。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
#define N 1007 string os1[N],os2[N];
int osind1[N],osind2[N];
int cost1[N],cost2[N];
pair<int,int> dp[N];
map<string,int> mp; int main()
{
int t,i,j,n,m;
int now;
string tmp;
scanf("%d",&t);
while(t--)
{
now = ;
mp.clear();
scanf("%d",&n);
for(i=;i<=n;i++)
{
cin>>tmp>>os1[i]>>cost1[i];
if(!mp[os1[i]])
mp[os1[i]] = now++;
osind1[i] = mp[os1[i]];
}
scanf("%d",&m);
for(i=;i<=m;i++)
{
cin>>tmp>>os2[i]>>cost2[i];
if(!mp[os2[i]])
mp[os2[i]] = now++;
osind2[i] = mp[os2[i]];
}
int maxi = max(n,m);
for(i=;i<=maxi;i++)
dp[i] = make_pair(,);
for(i=;i<=n;i++)
{
for(j=m;j>=;j--)
{
if(osind1[i] != osind2[j])
continue;
int k,num;
if(j >= )
{
k = dp[j-].first + cost1[i] + cost2[j];
num = dp[j-].second + ;
}
else
{
k = cost1[i] + cost2[j];
num = ;
}
if(dp[j].first < k)
{
dp[j].first = k;
dp[j].second = num;
}
else if(dp[j].first == k)
dp[j].second = min(dp[j].second,num);
}
for(j=;j<=m;j++)
{
if(dp[j].first < dp[j-].first)
dp[j] = dp[j-];
else if(dp[j].first == dp[j-].first && dp[j].second > dp[j-].second)
dp[j] = dp[j-];
}
}
printf("%d %d\n",dp[m].first,dp[m].second);
}
return ;
}
2014 Super Training #2 F The Bridges of Kolsberg --DP的更多相关文章
- 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
原题:ZOJ 3774 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...
- 2014 Super Training #6 F Search in the Wiki --集合取交+暴力
原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- 2014 Super Training #1 F Passage 概率DP
原题: HDU 3366 http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...
- 2014 Super Training #2 C Robotruck --单调队列优化DP
原题: UVA 1169 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 2014 Super Training #4 B Problem Arrangement --状压DP
原题:ZOJ 3777 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...
- 2014 Super Training #7 E Calculate the Function --矩阵+线段树
原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...
- 2014 Super Training #1 B Fix 状压DP
原题: HDU 3362 http://acm.hdu.edu.cn/showproblem.php?pid=3362 开始准备贪心搞,结果发现太难了,一直都没做出来.后来才知道要用状压DP. 题意: ...
- 2014 Super Training #8 B Consecutive Blocks --排序+贪心
当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...
随机推荐
- Java-链表LinkedList源码原理分析,并且通过LinkedList构建队列
在这里我们介绍一下最简单的链表LinkedList: 看一下add()方法: public boolean add(E e) { linkLast(e); return true; } void li ...
- rabbitmq学习笔记1 安装和配置
环境 OS: CentOS Linux release 7.1.1503 (Core) kernel:3.10.0-229.el7.x86_64 安装 参考:http://www.rabbitmq ...
- ajax提交复杂对象数据
public class RouteItemManageReq { private List<WorkNodeReq> targetNodes; private RouteItemReq ...
- 调用另一个Activity
<转>调用另一个Activity Intent对象的使用 范例说明 前一个范例介绍了如何运用切换Layout的方式,进行手机页面间的转换.如果要转换的页面并不单只是背景.颜色或文字内容的不 ...
- MAC下 JDK环境配置、版本切换以及ADB环境配置
网上方法,自己总结:亲测可行! 一.JDK环境配置.版本切换: 通过命令’jdk6′, ‘jdk7′,’jdk8’轻松切换到对应的Java版本: 1.首先安装所有的JDk:* Mac自带了的JDK6, ...
- handler机制的原理(转)
Handler概述 andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageEx ...
- HTML <!--...--> 注释 、CSS/JS //注释 和 /*.....*/ 注释
<!-- -->是HTML的注释标签,使用<和>是符合HTML标签语法规则的. /* */(注释代码块).//(注释单行)是CSS和JS的注释标签. 两种注释有各自的使用环境, ...
- andriod 资源文件之存取操作
来自:http://blog.csdn.net/jianghuiquan/article/details/8569235 <?xml version="1.0" encodi ...
- SQL SERVER – Attach mdf file without ldf file in Database
Background Story: One of my friends recently called up and asked me if I had spare time to look at h ...
- object-c中的类目,延展,协议
协议 协议只有方法的声明(类似于其他编程语言的接口) 协议相当于大家都所遵循的 关键字 @protocol 协议名 <所遵循的协议> 默认NSObject @end @pr ...