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

  1. Siki_Unity_3-3_背包系统

    Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...

  2. 杂项:大数据 (巨量数据集合(IT行业术语))

    ylbtech-杂项:大数据 (巨量数据集合(IT行业术语)) 大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉.管理和处理的数据集合,是需要新处理模式才能具有更强的决策力.洞 ...

  3. NP完全问题的证明

    目录 NP完全问题的证明 一.限制法 最小覆盖问题(VC) 子图同构问题 0-1背包(Knapsack) 三元集合的恰当覆盖(X3C) 集中集 有界度的生成树 多处理机调度 二.局部替换法 3SAT问 ...

  4. FOJProblem 2214 Knapsack problem(01背包+变性思维)

    http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4    Submit: 6Time Limit: 3000 mSec    Memory Lim ...

  5. FZU 2214 Knapsack problem 01背包变形

    题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...

  6. Atcoder E - Knapsack 2 (01背包进阶版 ex )

    E - Knapsack 2 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement The ...

  7. FZU 2214 ——Knapsack problem——————【01背包的超大背包】

    2214 Knapsack problem Accept: 6    Submit: 9Time Limit: 3000 mSec    Memory Limit : 32768 KB  Proble ...

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

  9. (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 ...

随机推荐

  1. Java并发简介

    年轻的时候学会了“使用”Servlet后,感觉自己什么都会做了,之后就不停的写所谓的业务逻辑,框架(这里说的不是structs,spring等,就是说servlet)给人们屏蔽了很多复杂性(更别说构建 ...

  2. SpringCloud IDEA 教学 (一) Eureka的简介与服务注册中心的建立

    写在开头 SpringCloud进来成为业界排名靠前的微服务框架,最核心功能就是搭建微服务,并在此基础上衍生出一系列功能,如断路器(Hystrix).断路监控.管理配置.Zuul.OAuth2等功能. ...

  3. Java实验二实验报告:java面向对象程序设计

    java实验二实验报告 实验内容 1. 初步掌握单元测试和TDD 2. 理解并掌握面向对象三要素:封装.继承.多态 3. 初步掌握UML建模 4. 熟悉S.O.L.I.D原则 5. 了解设计模式 实验 ...

  4. c# 读取xml文件 编写form

    主要思想:xml保存控件的数据,c#读取出来并加以显示. 难点:1.控件有父容器和子控件的关系:2.控件事件的添加. 1.控件有父容器和子控件的关系: 可以用绝对坐标在xml文件中先读取子控件再读取父 ...

  5. C语言特殊符号

    -> ->在C语言中称为间接引用运算符,是二目运算符,优先级同成员运算符“.”.用法:p->a,其中p是指向一个结构体的指针,a是这个结构体类型的一个成员.表达式p->a引用了 ...

  6. aria2 on ubuntu

    http://www.5yun.org/9102.html http://jpollo.logdown.com/posts/160847-aria2c-and-yaaw aria2c --enable ...

  7. osg::Vec2 Vec3 Vec4

    osg::Vec2可以用于保存2D纹理坐标. osg::Vec3是一个三维浮点数数组. osg::Vec4用于保存颜色数据.

  8. 201621123034 《Java程序设计》第5周学习总结

    作业05-继承.多态.抽象类与接口 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 答:关键字:接口.继承.多态 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般 ...

  9. 关闭win7/Server 2008非正常关机启动自动修复功能

    命令提示符下输入 bcdedit /set {default} bootstatuspolicy ignoreallfailures bcdedit /set {current} recoveryen ...

  10. php 生成短网址 代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...