Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e239ad

Browse files
author
shengshijun
committed
带权选择活动.
1 parent754e8e1 commit9e239ad

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ algorithm
2929
4. 加权有向无环图中最短路径和最长路径
3030

3131
###幂乘:算法复杂度是O(lgn)
32+
##贪心算法
33+
1. 活动选择问题
34+
2. 顶点作色问题
35+
3. 带权活动选择问题(其实就是一个调度问题)
3236

3337
###斐波那契树
3438
1. 使用循环实现的算法o(n)

‎dynamic/task/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__='shenshijun'

‎dynamic/task/max_weighted_action.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__='shenshijun'
4+
"""
5+
动态规划实现活动调度的问题,每个活动都有开始和结束时间,并且有一个权,要求得使得权和最大的活动集合
6+
"""
7+
8+
9+
classAction(object):
10+
"""
11+
活动对象
12+
"""
13+
14+
def__init__(self,start_time,end_time,weight):
15+
self.start_time=start_time
16+
self.end_time=end_time
17+
self.weight=weight
18+
19+
def__cmp__(self,other):
20+
pass
21+
22+
def__eq__(self,other):
23+
returnself.start_time==other.start_timeandself.end_time==other.end_time
24+
25+
def__hash__(self):
26+
returnhash(hash(self.start_time)*hash(self.end_time))
27+
28+
def__str__(self):
29+
return"".join(["Action(start_time=",str(self.start_time),",ent_time=",str(self.end_time),",weight=",
30+
str(self.weight),")"])
31+
32+
33+
__value_dict= {}
34+
__actions_dict= {}
35+
36+
37+
defmax_weighted_actions(action_list):
38+
"""
39+
假设Action是按照end_time排序的。因此在算法里面就不排序了。
40+
:param action_list:
41+
:return:
42+
"""
43+
global__actions_dict
44+
global__value_dict
45+
action_len=len(action_list)
46+
# 循环退出条件
47+
ifaction_lenis0:
48+
return0
49+
50+
cur_max_weight=0
51+
forkinxrange(action_len):
52+
left_max_weight=0
53+
ifk>0:
54+
if (action_list[0],action_list[k])notin__value_dict:
55+
__value_dict[(action_list[0],action_list[k])]=max_weighted_actions(
56+
filter(lambdaaction:action.end_time<=action_list[k].start_time,action_list[:k]))
57+
left_max_weight=__value_dict[(action_list[0],action_list[k])]
58+
59+
right_max_weight=0
60+
ifk<action_len-1:
61+
if (action_list[k],action_list[-1])notin__value_dict:
62+
__value_dict[(action_list[k],action_list[-1])]=max_weighted_actions(
63+
filter(lambdaaction:action.start_time>=action_list[k].end_time,action_list[k+1:]))
64+
right_max_weight=__value_dict[(action_list[k],action_list[-1])]
65+
66+
cur_weight=left_max_weight+right_max_weight+action_list[k].weight
67+
ifcur_max_weight<cur_weight:
68+
cur_max_weight=cur_weight
69+
70+
__value_dict[(action_list[0],action_list[-1])]=cur_max_weight
71+
return__value_dict[(action_list[0],action_list[-1])]
72+
73+
74+
defmain():
75+
action_list= [Action(1,4,2),Action(3,5,1),Action(0,6,1),Action(5,7,2),Action(3,9,1),Action(5,9,1),
76+
Action(6,10,1),Action(8,11,2),Action(8,12,1),Action(2,14,1),Action(2,16,2)]
77+
print(max_weighted_actions(action_list))
78+
79+
80+
if__name__=="__main__":
81+
main()
82+

‎greedy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python
2+
# -*- coding:UTF-8
3+
__author__='shenshijun'

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp