差分约束 poj 3169

Description

Windy has a country, and he wants to build an army to protect his country. He has picked up N girls and M boys and wants to collect them to be his soldiers. To collect a soldier without any privilege, he must pay 10000 RMB. There are some relationships between girls and boys and Windy can use these relationships to reduce his cost. If girl x and boy y have a relationship d and one of them has been collected, Windy can collect the other one with 10000-d RMB. Now given all the relationships between girls and boys, your assignment is to find the least amount of money Windy has to pay. Notice that only one relationship can be used when collecting one soldier.

Input

The first line of input is the number of test case.
The first line of each test case contains three integers, NM and R.
Then R lines followed, each contains three integers xiyi and di.
There is a blank line before each test case.

1 ≤ NM ≤ 10000
0 ≤ R ≤ 50,000
0 ≤ xi < N
0 ≤ yi < M
0 < di < 10000

Output

For each test case output the answer in a single line.

Sample Input

2

5 5 8
4 3 6831
1 3 4583
0 0 6592
0 1 3063
3 3 4975
1 3 2049
4 2 2104
2 2 781

5 5 10
2 4 9820
3 2 6236
3 1 8864
2 4 8326
2 0 5156
2 0 1463
4 1 2439
0 4 4373
3 4 8889
2 4 3133

Sample Output

71071
54223

Source

POJ Monthly Contest – 2009.04.05, windy7926778

这个题目也是个最短路的题目,但是题目数据需要处理。题目给出来了两个不等关系,d[al]+dl>=d[bl],d[ad]+dd<=d[bd],可以把他们转化一下,变成:d[al]+dl>=d[bl](即al向bl的有向边),d[bd]+(-dd)>=d[al](bd向al的有向边),因为对于起点和终点,或者说两相邻边,总有d[n]-d[s]<=w (w为某一路径长度),那么s到n的最短路就是这个w的最大值,即d[n]-d[s]的最大值,而这个路径,就是题目所求的最大距离。

即最短路求最大值,最大路求最小值。

这个题目还需要判断不存在,不存在的情况即存在负环,那么spfa的时候对每个点记录一下入队次数,大于n,说明存在环,标记输出-1。

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;

#define maxn 100005
#define INF 9999999
typedef pair<int,int> pii;
vector<pii> e[maxn];
int vis[maxn],cnt[maxn],d[maxn],n,ml,dl,flag;

void add_edge(int x,int y,int z)
{
    e[x].push_back(make_pair(y,z));
    //e[y].push_back(make_pair(x,z));
}

void init(int n)
{
    for(int i=0; i<=n; i++) e[i].clear();
    for(int i=0; i<=n; i++) d[i]=INF;
}

void spfa(int s)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    flag=0;
    q.push(s);
    d[s]=0;
    vis[s]=1;
    while(!q.empty())
    {
        int now=q.front();
        cnt[now]++;                    //记录每个点的入队次数
        if(cnt[now]>n)
        {
            flag=1;
            break;
        }
        vis[now]=0;
        q.pop();
        for(int i=0; i<e[now].size(); i++)
        {
            int v=e[now][i].first;
            if(d[v]>d[now]+e[now][i].second)
            {
                d[v]=d[now]+e[now][i].second;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v]=1;
                }
            }
        }
    }
    if(flag)
        printf("-1\n");
    else if(d[n]==INF)
        printf("-2\n");
    else
        printf("%d\n",d[n]);

}

int main()
{
    while(~scanf("%d %d %d",&n,&ml,&dl))
    {
        init(n);
        for(int i=0; i<ml; i++)
        {
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            add_edge(x,y,z);
        }
        for(int i=0; i<dl; i++)
        {
            int x,y,z;
            scanf("%d %d %d",&x,&y,&z);
            add_edge(y,x,-z);                        //注意存边顺序,这个是个有向图,方向很重要
        }
        for(int i=1;i<n;i++) e[i+1].push_back(make_pair(i,0));   //后一头牛一定在前一头牛前面 (其实不写也行,因为初始化的都是0,不可能出现负数)
        spfa(1);
    }
}

原文地址:https://www.cnblogs.com/youchandaisuki/p/8665289.html

时间: 2024-10-11 19:45:35

差分约束 poj 3169的相关文章

差分约束Poj 3169 Layout

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 #include <cstdio> #include <

poj 3169 Layout (差分约束+Bellman )

题目链接:http://poj.org/problem?id=3169 题意:输入N, ML, MD, N默示有N个牛按1-N排成一排,ML,默示有ML行,每行输入A, B, D默示A牛和B牛最远间隔为D, MD默示有MD行,每行输入A,B,D默示A牛和B来间隔为D,求满足所有前提的1-N的最大间隔. 比较简单的差分约束,这个周周赛的A题 #include <iostream> #include <cstdlib> #include <cstdio> #include

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

poj 3169 差分约束

3169 差分约束的是满足多组形如xi-yj<=bk{i,j<n k<m}不等式极值问题,可以转化为单源最短路来求. 在最短路中 d[v]<=d[u]+w(u,v) 可以看出跟上面的不等式很像 通常有两种一种是求满足所有不等式的最大值,还有是最小值. 这篇博客可以参考一下 分析: 题目给出了两种不等式  d[u]+dl >=d[v]       d[u]+dd<=d[v]  要求的是最大值,也就是最短路 对于d[u]+dl>=d[v] 建边(u,vdl) 对于d[

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ 3169 Layout (差分约束)

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0,  D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了. 1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

POJ 3169 Layout(差分约束系统)

题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ML组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w.2.有MD组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w.问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最