zoukankan      html  css  js  c++  java
  • hdu 4109 Instrction Arrangement (差分约束)

     1 差分约束系统
     2 加一个源点s指向所有点边权为0,
     3 加一个汇点t,所有点指向t边权为0,
     4 u和v安全距离为w,则加边v->u,边权为-w
     5 (因为 u-v>=w 所以v-u<=-w )
     6 求s到t最短路即可。
     7 http://acm.hdu.edu.cn/showproblem.php?pid=4109
     8 #include<stdio.h>
     9 const int N=10000;
    10 #define max 999999
    11 int dis[N],num,n;
    12 struct node
    13 {
    14     int b;
    15     int e;
    16     int w;
    17 
    18 }p[N];
    19 void init()
    20 {
    21     int i;
    22     for(i=0;i<=n+1;i++)
    23        dis[i]=0;
    24 }
    25 void insert(int x,int y,int z)
    26 {
    27             p[num].b=x;
    28             p[num].e=y;
    29             p[num].w=z;
    30             num++;
    31 }
    32 void bellman()
    33 {
    34     int i,j;
    35 
    36     for(i=1;i<=n+1;i++)
    37     {
    38         for(j=0;j<num;j++)
    39         {
    40             if(dis[p[j].e]>dis[p[j].b]+p[j].w)
    41             {
    42                 dis[p[j].e]=dis[p[j].b]+p[j].w;
    43             }
    44         }
    45     }
    46 }
    47 int main()
    48 {
    49     int m,i,x,y,z;
    50     while(scanf("%d%d",&n,&m)!=EOF)
    51     {
    52         num=0;
    53         init();
    54         for(i=1;i<=m;i++)
    55         {
    56             scanf("%d%d%d",&x,&y,&z);
    57              insert(x+1,y+1,-z);
    58 
    59         }
    60 
    61         for(i=1;i<=n;i++)
    62         {
    63             insert(0,i,0);
    64             insert(i,n+1,0);
    65         }
    66         bellman();
    67         //for(i=0;i<=n+1;i++)printf("%d\n",dis[i]);
    68         printf("%d\n",1-dis[n+1]);
    69 
    70     }
    71 }
  • 相关阅读:
    Go Map
    Go XORM
    Go切片
    Go函数
    dockerfile常用指令
    Goroutine并发控制
    Go 格式转换
    Go 常用知识点及实例
    Go 时间
    Go error
  • 原文地址:https://www.cnblogs.com/acSzz/p/2468436.html
Copyright © 2011-2022 走看看