美国选举问题/完全背包/Knapsack
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Knapsack
{
/// <summary>
/// Project:Knapsack
/// Author: Andy Zeng
/// Email:andyzengsh@gmail.com
/// Date:2014/04/12
/// </summary>
class Program
{
static void Main(string[] args)
{
InitStates();
int totalNumber = (States.Sum(s => s.TicketCount));
if (totalNumber % == )
Console.WriteLine("No Possible to Equal!");
int[,] Best = new int[States.Length + , totalNumber / + ];
Console.WriteLine("Target is {0}.", totalNumber / );
for (int i = ; i <= States.Length - ; i++)//
{
for (int j = ; j <= totalNumber / ; j++)
{
if (j < States[i].TicketVolume)
{
Best[i, j] = Best[i - , j];
}
else
{
int tmp = Best[i - , j - States[i].TicketVolume] + States[i].TicketCount;
int tmp2 = Best[i - , j];
Best[i, j] = Math.Max(tmp, tmp2);
}
}
}
Console.WriteLine("The best result for target {0} is {1}.", totalNumber / , Best[States.Length - , totalNumber / ]); List<State> tmpStates = new List<State>(); int lastMax = States.Length - ;
int lastTarget = totalNumber / ;
while (lastMax > && lastTarget > && Best[lastMax, lastTarget] > )
{
int currentMax = lastMax;
for (int i = lastMax; i > ; i--)
{
if (Best[i, lastTarget] >= Best[lastMax, lastTarget])
{ currentMax = i; }
} if (lastMax >= currentMax)
{
tmpStates.Add(States[currentMax]);
lastMax = currentMax;
lastTarget = lastTarget - States[currentMax].TicketCount;
lastMax--;
}
else
{
tmpStates.Add(States[lastMax]);
lastTarget = lastTarget - States[lastMax].TicketVolume;
lastMax--;
}
} tmpStates.ForEach(s => { Console.WriteLine(s); });
Console.WriteLine("TotalTickets:{0}", tmpStates.Sum(s => s.TicketCount)); Console.ReadKey();
} //Remember:i And v are both dynamic values.
//F(i,v)=f(i-1,v) ,At this condition ,the number i thing's volume is biggger than the v
//or
//F(i,v)=Max{F(i-1,v),F(i-1,v-C(i))+W(i)},pick the biggest/best result private static State[] States;
private static void InitStates()
{
var states = baseData.Split(',');
States = new State[states.Length + ];
States[] = new State() { Name = string.Empty, TicketCount = };
for (int i = ; i <= states.Length; i++)
{
var tmpStrings = states[i - ].Split(':');
States[i] = new State() { Name = tmpStrings[].Trim(), TicketCount = int.Parse(tmpStrings[]) };
}
}
#region baseData
private const string
baseData = @"
阿拉巴马州:9,
阿拉斯加州:3,
亚利桑那州:10,
阿肯色州:6,
加利福尼亚州:55,
科罗拉多州:9,
康涅狄格州:7,
特拉华州:3,
哥伦比亚特区:3,
佛罗里达州:27,
乔治亚州:15,
夏威夷州:4,
爱达荷州:4,
伊利诺伊州:21,
印第安那州:11,
艾奥瓦州:7,
堪萨斯州:6,
肯塔基州:8,
路易斯安那州:9,
缅因州:4,
马里兰州:10,
马萨诸塞州:12,
密歇根州:17,
明尼苏达州:10,
密西西比州:6,
密苏里州:11,
蒙达拿州:3,
内布拉斯加州:5,
内华达州:5,
新罕布什尔州:4,
新泽西州:15,
新墨西哥州:5,
纽约州:31,
北卡罗来纳州:15,
北达科他州:3,
俄亥俄州:20,
俄克拉荷马州:7,
俄勒岗州:7,
宾夕法尼亚州:21,
罗德岛州:4,
南卡罗来纳州:8,
南达科达州:3,
田纳西州:11,
德克萨斯州:34,
犹他州:5,
蒙大拿州:3,
维吉尼亚州:13,
华盛顿州:11,
西维吉尼亚州:5,
威斯康辛州:10,
怀俄明州:3";
#endregion
}
class State
{
public string Name { get; set; }
public int TicketCount { get; set; }
//In this issue , the Volume property is to simulate the Volume property from the Package Algorithm.
public int TicketVolume { get { return TicketCount; } }
public override string ToString()
{
return string.Format("{0}:{1}", Name, TicketCount);
}
}
}
运行结果:

下载源码。
作者:Andy Zeng
欢迎任何形式的转载,但请务必注明出处。
http://www.cnblogs.com/andyzeng/p/3670397.html
美国选举问题/完全背包/Knapsack的更多相关文章
- Siki_Unity_3-3_背包系统
Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...
- 杂项:大数据 (巨量数据集合(IT行业术语))
ylbtech-杂项:大数据 (巨量数据集合(IT行业术语)) 大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉.管理和处理的数据集合,是需要新处理模式才能具有更强的决策力.洞 ...
- NP完全问题的证明
目录 NP完全问题的证明 一.限制法 最小覆盖问题(VC) 子图同构问题 0-1背包(Knapsack) 三元集合的恰当覆盖(X3C) 集中集 有界度的生成树 多处理机调度 二.局部替换法 3SAT问 ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- Atcoder E - Knapsack 2 (01背包进阶版 ex )
E - Knapsack 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...
- FZU 2214 ——Knapsack problem——————【01背包的超大背包】
2214 Knapsack problem Accept: 6 Submit: 9Time Limit: 3000 mSec Memory Limit : 32768 KB Proble ...
- FZU - 2214 Knapsack problem 01背包逆思维
Knapsack problem Given a set of n items, each with a weight w[i] and a value v[i], determine a way t ...
- (01背包 当容量特别大的时候) Knapsack problem (fzu 2214)
http://acm.fzu.edu.cn/problem.php?pid=2214 Problem Description Given a set of n items, each with a ...
随机推荐
- Tensorflow学习第1课——从本地加载MNIST以及FashionMNIST数据
很多Tensorflow第一课的教程都是使用MNIST或者FashionMNIST数据集作为示例数据集,但是其给的例程基本都是从网络上用load_data函数直接加载,该函数封装程度比较高,如果网络出 ...
- [C++] Copy Control (part 1)
Copy, Assign, and Destroy When we define a class, we specify what happens when objects of the class ...
- C++基础和STL,Effective C++笔记
这个作者总结的c++基础,特别好. 可以看看. http://blog.csdn.net/tham_/article/details/51169792
- JavaBean中DAO设计模式简介
一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/Ser ...
- python实现post请求
今天无论如何都要留下一些什么东西... 可以说今天学到一个新的一个东西,也需要分享出来,给更多的人去使用. 今天爬取的数据里面是客户端向服务器端发送加密过的token和一些页码之类的一个数据.(我主要 ...
- 201621044079 韩烨 week11-作业11-多线程
作业11-多线程 参考资料 多线程参考文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序 ...
- VMbox复制虚拟机后网卡问题-bring up interface eth0:Device eth0 does not seem to be present
1.使用 ifconfig -a 查看mac地址 eg:HWaddr:08:00:29:B2:2B 2.vi /etc/sysconfig/network-scripts/ifcfg-eth0 将 ...
- 【Python】python动态类型
在python中,省去了变量声明的过程,在引用变量时,往往一个简单的赋值语句就同时完成了,声明变量类型,变量定义和关联的过程,那么python的变量到底是怎样完成定义的呢? 动态类型 python使用 ...
- jsp文件过大,is exceeding 65535 bytes limit
今天修改配置项的时候,遇到了一个异常,Generated servlet error:The code of method _jspService(HttpServletRequest, HttpSe ...
- RT-thread内核之异常与中断
一.什么是中断? 中断有两种,一种是CPU本身在执行程序的过程中产生的,一种是由CPU外部产生的. cpu外部中断,就是通常所讲的“中断”(interrupt).对于执行程序来说,这种“中断”的发生完 ...