最短ルートのベルマンアルゴリズム

4854 ワード

ベルマンアルゴリズム:
各辺に対して弛緩操作を行い、すなわち最短距離2を更新し、回路が発生したか否かを判断する
 
Bellman-Fordアルゴリズムの流れは以下の通りである:所与の図G(V,E)(ここでV,Eはそれぞれ図Gの頂点セットと辺セットである)、ソース点s,配列Distant[i]はソース点sから頂点iまでの経路長を記録し、初期化配列Distant[n]は、Distant[s]は0である.
以下の動作サイクルは、n-1回まで実行され、nは頂点数である.各辺e(u,v)について、Distant[u]+w(u,v) 
例:
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Line 1: A single integer, F. F farm descriptions follow. Line 1 of each farm: Three space-separated integers respectively: N, M, and W Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path. Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1..F: For each farm, output "YES"if FJ can achieve his goal, otherwise output "NO"(do not include the quotes).
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8

Sample Output
NO YES

テーマ:
FJの子供靴家の畑にはいくつかの虫の穴があり、いくつかの虫の穴がつながっていて、いくつかの時間を費やして登ることができます(方向性がありません).一部の虫の穴は特殊で、片方から片方に登った後、時間が少し後退します(方向性があります)
FJは爬虫穴(?)を通って来た時に時間が後退して、もう一人の自分を見ることができます.入力された虫穴情報に基づいてFJの願いが叶うか否かを判断する.
code:
  #include#define inf 999999struct node{    int u,v,w;} edge[6000];int dis[505];int n,m,w,in;void add(int u,int v,int c){    in++;    edge[in].u=u;    edge[in].v=v;    edge[in].w=c;}int bellman(){int u,v,w,i,j,flag;for(i=1;i<=n;i+)/初期化{dis[i]=inf;    }    dis[1]=0;    flag=0;for(i=1;i<=n;i++)/エッジの弛緩{for(j=1;j<=in;j+){
             if(dis[edge[j].v]>dis[edge[j].u]+edge[j].w)            {                dis[edge[j].v]=dis[edge[j].u]+edge[j].w;}}}for(i=1;i<=in;i++)/ループif(dis[edge[i].v]>dis[edge[i].u]+edge[i].w)return 1があるか否かを判断する;    return 0;}int main(){    int i,j,t,u,v,c;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d",&n,&m,&w);        in=0;        for(i=0; i単一ソース最短パスアルゴリズムとして、Bellman-Fordは有向図と無向図の両方に適用でき、Dijkstraアルゴリズムでは備えられない特徴があり、それは負の重みを含む最短パス検索である.各i輪対エッジの遍歴後、負の重み回路が存在しない限り、Bellman−Fordアルゴリズムは、ソース点iのエッジからの点の最短経路を得ることを保証することができる.最短パスの最大長はn辺を超えないため、nはノードの数である.したがって,n回の遍歴後,すべてのノードは必ず最短経路を見つけることができる.n回後も緩和を続けることができれば,この図には負の重みループが存在し,コードを少し修正して負の重みループのサイズを計算できることを示した.