zoukankan      html  css  js  c++  java
  • 差分约束系统 + spfa(A

    题目链接:https://cn.vjudge.net/contest/276233#problem/A

    差分约束系统,假设当前有三个不等式

    x- y <=t1

    y-z<=t2

    x-z<=t3

    我们可以将第一个式子和第二个式子结合起来,就变成了x-z<= t1+t2 ,然后x-z的最大差值就是min(t1+t2,t3)(因为要使得最终结果都满足两个不等式)

    然后求最小的过程(求差最大),就可以通过最短路的算法实现。

    题目大意:给你n代表有n头牛,然后ml和md,接下来ml行,每行有三个数u v w代表u和v之间的距离最多是w,接下来md行,每行有三个数,代表u v 之间的距离最少是w,然后问你第一个牛和第n个牛最远可以相差多少,如果是无穷远输出-2.如果没有满足的情况,输出-1,否则输出dis【n】。

     具体思路:我们可以将题目条件转换为不等式进行求解,对于第一种情况,也就是ml的时候,我们可以转成如下式子

    posU-posV < = w。然后我们就可以连一条边,u->v (权值是w),对于第二种情况,我们转化的式子是posU-posV>=w,我们需要将这个式子转换成和第一种形式相同的,所以两边同乘-1,就变成了posV-posU<=-w,然后就是建边就可以了。

    AC代码:

     1 #include<iostream>
     2 #include<cstring>
     3 #include<stack>
     4 #include<iomanip>
     5 #include<cmath>
     6 #include<queue>
     7 #include<algorithm>
     8 #include<stdio.h>
     9 using namespace std;
    10 # define ll long long
    11 # define inf 0x3f3f3f3f
    12 const int maxn = 1e6+100;
    13 int n,ml,md;
    14 int num,head[maxn],dis[maxn],out[maxn],vis[maxn];
    15 struct node
    16 {
    17     int fr;
    18     int to;
    19     int cost;
    20     int nex;
    21 } edge[maxn];
    22 void init()
    23 {
    24     num=0;
    25     memset(head,-1,sizeof(head));
    26 }
    27 void addedge(int fr,int to,int cost)
    28 {
    29     edge[num].to=to;
    30     edge[num].cost=cost;
    31     edge[num].nex=head[fr];
    32     head[fr]=num++;
    33 }
    34 int spfa()
    35 {
    36     queue<int>q;
    37     q.push(1);
    38     memset(dis,inf,sizeof(dis));
    39     dis[1]=0;
    40     vis[1]=1;
    41     out[1]++;
    42     while(!q.empty())
    43     {
    44         int tmp=q.front();
    45         out[tmp]++;
    46         if(out[tmp]>n)//判断会不会成负环
    47             return -1;
    48         q.pop();
    49         vis[tmp]=0;
    50         for(int i=head[tmp]; i!=-1; i=edge[i].nex)
    51         {
    52             int u=edge[i].to;
    53             if(dis[u]>dis[tmp]+edge[i].cost)
    54             {
    55                 dis[u]=dis[tmp]+edge[i].cost;
    56                 if(vis[u])
    57                     continue;
    58                 q.push(u);
    59                 vis[u]=1;
    60             }
    61         }
    62     }
    63     if(dis[n]==inf)
    64         return -2;
    65     return dis[n];
    66 }
    67 int main()
    68 {
    69     init();
    70     scanf("%d %d %d",&n,&ml,&md);
    71     int u,v,w;
    72     for(int i=1; i<=ml; i++)
    73     {
    74         scanf("%d %d %d",&u,&v,&w);
    75         addedge(u,v,w);
    76     }
    77     for(int i=1; i<=md; i++)
    78     {
    79         scanf("%d %d %d",&u,&v,&w);
    80         addedge(v,u,-w);
    81     }
    82     int ans=spfa();
    83     printf("%d
    ",ans);
    84     return 0;
    85 }
    86  
  • 相关阅读:
    Codeforces 834D The Bakery
    hdu 1394 Minimum Inversion Number
    Codeforces 837E Vasya's Function
    Codeforces 837D Round Subset
    Codeforces 825E Minimal Labels
    Codeforces 437D The Child and Zoo
    Codeforces 822D My pretty girl Noora
    Codeforces 799D Field expansion
    Codeforces 438D The Child and Sequence
    Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D)
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10262771.html
Copyright © 2011-2022 走看看