最短路径问题的Dijkstra算法 -python
最短路径问题的Dijkstra算法
是由荷兰计算机科学家艾兹赫尔·戴克斯特拉提出。迪科斯彻算法使用了广度优先搜索解决非负权有向图的单源最短路径问题,算法最终得到一个最短路径树> 。该算法常用于路由算法或者作为其他图算法的一个子模块。
# Dijkstra's algorithm for shortest paths # David Eppstein, UC Irvine, 4 April 2002 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/117228 from priodict import priorityDictionary def Dijkstra(G,start,end=None): """ Find shortest paths from the start vertex to all vertices nearer than or equal to the end. The input graph G is assumed to have the following representation: A vertex can be any object that can be used as an index into a dictionary. G is a dictionary, indexed by vertices. For any vertex v, G[v] is itself a dictionary, indexed by the neighbors of v. For any edge v->w, G[v][w] is the length of the edge. This is related to the representation in <http://www.python.org/doc/essays/graphs.html> where Guido van Rossum suggests representing graphs as dictionaries mapping vertices to lists of outgoing edges, however dictionaries of edges have many advantages over lists: they can store extra information (here, the lengths), they support fast existence tests, and they allow easy modification of the graph structure by edge insertion and removal. Such modifications are not needed here but are important in many other graph algorithms. Since dictionaries obey iterator protocol, a graph represented as described here could be handed without modification to an algorithm expecting Guido's graph representation. Of course, G and G[v] need not be actual Python dict objects, they can be any other type of object that obeys dict protocol, for instance one could use a wrapper in which vertices are URLs of web pages and a call to G[v] loads the web page and finds its outgoing links. The output is a pair (D,P) where D[v] is the distance from start to v and P[v] is the predecessor of v along the shortest path from s to v. Dijkstra's algorithm is only guaranteed to work correctly when all edge lengths are positive. This code does not verify this property for all edges (only the edges examined until the end vertex is reached), but will correctly compute shortest paths even for some graphs with negative edges, and will raise an exception if it discovers that a negative edge has caused it to make a mistake. """ D = {} # dictionary of final distances P = {} # dictionary of predecessors Q = priorityDictionary() # estimated distances of non-final vertices Q[start] = 0 for v in Q: D[v] = Q[v] if v == end: break for w in G[v]: vwLength = D[v] + G[v][w] if w in D: if vwLength < D[w]: raise ValueError, "Dijkstra: found better path to already-final vertex" elif w not in Q or vwLength < Q[w]: Q[w] = vwLength P[w] = v return (D,P) def shortestPath(G,start,end): """ Find a single shortest path from the given start vertex to the given end vertex. The input has the same conventions as Dijkstra(). The output is a list of the vertices in order along the shortest path. """ D,P = Dijkstra(G,start,end) Path = [] while 1: Path.append(end) if end == start: break end = P[end] Path.reverse() return Path # example, CLR p.528 G = {'s': {'u':10, 'x':5}, 'u': {'v':1, 'x':2}, 'v': {'y':4}, 'x':{'u':3,'v':9,'y':2}, 'y':{'s':7,'v':6}} print Dijkstra(G,'s') print shortestPath(G,'s','v')
广州市养老金计算方法(多实例) - 广州本地宝
广州基础养老金、个人账户养老金、过渡性养老金、过渡性调节金分别按下列办法计算:
定义:以当地上年度在岗职工月平均工资和本人指数化月平均缴费工资的平均值为基数,缴费每满1年发给1%。
计算公式为:基础养老金=(参保人员退休时当地上年度在岗职工月平均工资+本人指数化月平均缴费工资)÷2×缴费年限×1%
其中:指数化月平均缴费工资=当地上年度在岗职工月平均工×本人平均缴费指数
个人的平均缴费指数: 每年的缴费基数与前年的当地月平均工资的比值 全部加起来求平均。 指数化月平均缴费工资,就是上面求得那个值乘以退休时的当地月平均工资。 比如:2000年当地月平均为1000,2001年你月交纳基数为2000,那么那一年的指数是2。如果你一直这么交,每年的指数都是2,最后退休的时候,平均指数也是2。 那么本人指数化月平均缴费工资就是2*退休时当地月平均工资。 基础养老金计算举例: 题目:李某07到11年,每年养老保险的个人实际缴费基数和当年的社会平均工资分别假设为,07年3000和3500,08年3200和4000,09年3500和4200,10年3600和4200,11年3800和4500,12年年初开始,李某开始领取养老保险金。而11年当地社会平均工资为4500元,他退休年龄为60岁,当时个人账户总储值为50000元,缴纳年度一共为15年。 1、计算个人平均缴费指数 那么李某07到11年这5年时间的个人平均缴费指数,应该这么计算 个人平均缴费指数=(07年个人实际缴费基数/07年社会平均工资+08年个人实际缴费基数/08年社会平均工资+09年···+11年个人实际缴费基数/11年社会平均工资)÷5 2、计算指数化月平均缴费工资 指数化月平均缴费工资=上年度社会平均工资×本人平均缴费指数=4500×0.838=3771 3、计算基础养老金 基础养老金=(上年度社会月平均工资+本人指数化月平均缴费工资)÷2×缴费年限×1%=(4500+3771)÷2×15×1%=620元 注:本例子简化计算,实际计算过程中,对于个人平均缴费指数的计算年数,至少应该为15年。 计算公式为:个人账户养老金=参保人员退休时个人账户累计储存额÷计发月数 注:计发月数定义 国家有一个统计,人的平均寿命(男女不同)。比如:男的平均寿命80岁,退休年龄为60岁。 那么通常一个男的退休要领20*12=240个月的退休金。个人账户的钱就要按240个月领完来计算每个月发多少。 但是个人账户还是有利息的。所以根据每年的利息,还有240个月的长度,会得到一个计发月数。 即个人账户/计发月数 就是每个月可以从个人账户领取的钱,保证你240个月领空。 如果万一活得长了,社保局就要从统筹基金里拿出钱来贴补了。 目前实施数据:我国目前实行的计发月数,分别为40岁233,50岁195,60岁139等等,具体查看这里:个人养老金计发月数表(2012) 个人账户养老金举例 例如:李某退休时个人账户累计储存额为50000元,他是60岁退休的,个人账户养老金=50000÷139=360元。 若按上面两个例子的结合,那么李某每个月能领到的养老金=基础养老金账户+个人账户=576+360=936元。 说明缴费年限和个人平均缴费基数对养老金影响的例子:根据上述公式,假定男职工在60岁退休时,全省上年度在岗职工月平均工资为4000元。 累计缴费年限为15年时: 个人平均缴费基数为0.6时,基础养老金=(4000元+4000元×0.6)÷2×15×1%=480元 个人平均缴费基数为1.0时,基础养老金=(4000元+4000元×1.0)÷2×15×1%=600元 个人平均缴费基数为3.0时,基础养老金=(4000元+4000元×3.0)÷2×15×1%=1200元 累计缴费年限为40年时: 个人平均缴费基数为0.6时,基础养老金=(4000元+4000元×0.6)÷2×40×1%=1280元 个人平均缴费基数为1.0时,基础养老金=(4000元+4000元×1.0)÷2×40×1%=1600元 个人平均缴费基数为3.0时,基础养老金=(4000元+4000元×3.0)÷2×40×1%=3200元
=(3000/3500+3200/4000+3500/4200+3600/4200+3800/4500)÷5=0.838
Chart.js Documentation
Chart interactivity
If you are looking to add interaction as a layer to charts, Chart.js is not the library for you. A better option would be using SVG, as this will let you attach event listeners to any of the elements in the chart, as these are all DOM nodes.
Chart.js uses the canvas element, which is a single DOM node, similar in characteristics to a static image. This does mean that it has a wider scope for compatibility, and less memory implications than SVG based charting solutions. The canvas element also allows for saving the contents as a base 64 string, allowing saving the chart as an image.
In SVG, all of the lines, data points and everything you see is a DOM node. As a result of this, complex charts with a lot of intricacies, or many charts on the page will often see dips in performance when scrolling or generating the chart, especially when there are multiple on the page. SVG also has relatively poor mobile support, with Android not supporting SVG at all before version 3.0, and iOS before 5.0. (caniuse.com/svg-html5).
Browser support
Browser support for the canvas element is available in all modern & major mobile browsers (caniuse.com/canvas).
For IE8 & below, I would recommend using the polyfill ExplorerCanvas - available at https://code.google.com/p/explorercanvas/. It falls back to Internet explorer's format VML when canvas support is not available. Example use:
<head> <!--[if lte IE 8]> <script src="excanvas.js"></script> <![endif]--> </head>
Usually I would recommend feature detection to choose whether or not to load a polyfill, rather than IE conditional comments, however in this case, VML is a Microsoft proprietary format, so it will only work in IE.
Some important points to note in my experience using ExplorerCanvas as a fallback.
- Initialise charts on load rather than DOMContentReady when using the library, as sometimes a race condition will occur, and it will result in an error when trying to get the 2d context of a canvas.
- New VML DOM elements are being created for each animation frame and there is no hardware acceleration. As a result animation is usually slow and jerky, with flashing text. It is a good idea to dynamically turn off animation based on canvas support. I recommend using the excellentModernizr to do this.
- When declaring fonts, the library explorercanvas requires the font name to be in single quotes inside the string. For example, instead of your scaleFontFamily property being simply "Arial", explorercanvas support, use "'Arial'" instead. Chart.js does this for default values.