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

  1. 2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂

    原题:ZOJ 3774  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774 --------------------- ...

  2. 2014 Super Training #6 F Search in the Wiki --集合取交+暴力

    原题: ZOJ 3674 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3674 题意不难理解,很容易想到用暴力,但是无从下 ...

  3. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  4. 2014 Super Training #1 F Passage 概率DP

    原题: HDU 3366   http://acm.hdu.edu.cn/showproblem.php?pid=3366 本来用贪心去做,怎么都WA,后来看网上原来是一个DP题. 首先按P/Q来做排 ...

  5. 2014 Super Training #2 C Robotruck --单调队列优化DP

    原题: UVA 1169  http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  6. 2014 Super Training #4 B Problem Arrangement --状压DP

    原题:ZOJ 3777  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3777 题意:给每个题目安排在每个位置的value ...

  7. 2014 Super Training #7 E Calculate the Function --矩阵+线段树

    原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...

  8. 2014 Super Training #1 B Fix 状压DP

    原题: HDU 3362 http://acm.hdu.edu.cn/showproblem.php?pid=3362 开始准备贪心搞,结果发现太难了,一直都没做出来.后来才知道要用状压DP. 题意: ...

  9. 2014 Super Training #8 B Consecutive Blocks --排序+贪心

    当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...

随机推荐

  1. 实现跨域请求jsonp方式

    原理:http://madong.net.cn/index.php/2012/12/368/ 调用端: $.getJSON("http://192.168.220.85:8001/esb/a ...

  2. css设置height 100%

    需要显式设置html,body为100%,body是相对于html,wrapper是相对于body html,body{ height: 100%; } .wrapper{ height: 100; ...

  3. DevExpress.XtraGrid.Views 设置指定行的背景颜色 .

    如需要将指定行的背景设置颜色,可参考以下示例 1.事件:CustomDrawCell 2.示例: private void gridView1_CustomDrawCell(object sender ...

  4. SharePoint 2013 隐藏部分Ribbon菜单

    SharePoint的使用中,因为用户经常不愿意看到那些不经常使用的操作,我们经常需要定制Ribbon菜单, 更多时候不是隐藏所有,而是隐藏掉我们不需要的那些:下面,我们一列表为例,简单介绍下如何部分 ...

  5. UtilDev Web Server Pro

    A good tool for asp.net hosting: http://ultidev.com/products/UWS-Cassini-Pro/

  6. iOS- 利用AFNetworking3.0+(最新AFN) - 实现文件断点下载

    官方建议AFN的使用方法   0.导入框架准备工作 •1. 将AFNetworking3.0+框架程序拖拽进项目   •2. 或使用Cocopod 导入AFNetworking3.0+   •3.   ...

  7. 关于tableView中tableHeaderView/tableFooterView/sectionHeader/sectionFooter/contentInset的理解

    其实每个人的理解有所不同,找到最有利于自己的理解方式即可.有人把Cell,tableHeaderView,tableFooterView,sectionHeader,sectionFooter这些属性 ...

  8. OC小实例关于init方法不小心的错误

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. 转 Android Dalvik虚拟机初识

    首先,让我们来思考下面几个问题: 什么是Dalvik虚拟机? Dalvik VM与JVM有什么区别? Dalvik VM有什么新的特点? Dalvik VM的架构是怎么样的? 首先,我得承认第一个问题 ...

  10. Apache Shiro 简介

    使用 Apache Shiro 为 web 应用程序进行用户身份验证 Shiro 是一个 Apache Incubator 项目,旨在简化身份验证和授权.在本文中,了解 Apache Shiro 并通 ...