原题: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. OpenGL 圆角矩形

    本来打算用四个圆角GL_TRIANGLE_FANS+两个矩形来填充, 后来经无情公子的提醒, 突然发现:"尼玛就是一压扁了的圆啊!" 于是全部用GL_TRIANGLE_FANS, ...

  2. (旧)子数涵数·C语言——条件语句

    首先,我们讲一下理论知识,在编程中有三种结构,分别是顺序结构.条件结构.循环结构,如果用流程图来表示的话就是: 那么在C语言中,如何灵活运用这三种结构呢?这就需要用到控制语句了. 而条件语句便是控制语 ...

  3. ahjesus用forever管理nodejs服务

    全局安装forever npm install -g forever 查看帮助 forever -h 查看安装位置 whereis forever 编写自己的sh文件 forever -p web文件 ...

  4. 利用多写Redis实现分布式锁原理与实现分析(转)

    利用多写Redis实现分布式锁原理与实现分析   一.关于分布式锁 关于分布式锁,可能绝大部分人都会或多或少涉及到. 我举二个例子:场景一:从前端界面发起一笔支付请求,如果前端没有做防重处理,那么可能 ...

  5. VS 2013 Preview 自定义 SharePoint 2013 列表 之 两个Bug

    SharePoint 2013 已RTM了,对于程序员来说又要了解新功能了,同时 VS 2013 也将要 RTM了,两者同时应用定会有不新功能,我们先从 自定义 列表开始. SharePoint 20 ...

  6. jquery实现轮播

    HTML代码: <div class="ad"> <ul class="slider"> <li><img src=& ...

  7. js中this的理解

    平常用this很多,对this的理解就是this是对应执行环境,然而很多时候效果并不是想要的,最近看了一些谈到this的笔记和书籍,总结下. 对this的误解: this是指向函数本身 先上个demo ...

  8. android Java BASE64编码和解码二:图片的编码和解码

    1.准备工作 (1)在项目中集成 Base64 代码,集成方法见第一篇博文:android Java BASE64编码和解码一:基础 (2)添加 ImgHelper 工具类 package com.a ...

  9. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  10. android 跳转到系统设置界面的所有Intent

    Intent 的 意图: Intent intent = new Inetnt(Setings); Setings: 1. ACTION_ACCESSIBILITY_SETTINGS : // 跳转系 ...