码迷,mamicode.com
首页 > 其他好文 > 详细

差分约束 poj 3169

时间:2018-03-28 18:52:48      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:bre   org   情况   bsp   size   差分约束   oid   memset   ships   

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

 
这个题目也是个最短路的题目,但是题目数据需要处理。题目给出来了两个不等关系,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);
    }
}
View Code

 

差分约束 poj 3169

标签:bre   org   情况   bsp   size   差分约束   oid   memset   ships   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!