K P1993 small farms (differential constraints)

K small farms


题目描述

小K在MC里面建立很多很多的农场,总共n个,以至于他自己都忘记了每个农场中种植作物的具体数量了,他只记得一些含糊的信息(共m个),以下列三种形式描述:
    农场a比农场b至少多种植了c个单位的作物,
    农场a比农场b至多多种植了c个单位的作物,
    农场a与农场b种植的作物数一样多。
但是,由于小K的记忆有些偏差,所以他想要知道存不存在一种情况,使得农场的种植作物数量与他记忆中的所有信息吻合。
输入输出格式
输入格式:

第一行包括两个整数 n 和 m,分别表示农场数目和小 K 记忆中的信息数目。

接下来 m 行:

如果每行的第一个数是 1,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至少多种植了 c 个单位的作物。

如果每行的第一个数是 2,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至多多种植了 c 个单位的作物。如果每行的第一个数是 3,接下来有 2 个整数 a,b,表示农场 a 种植的的数量和 b 一样多。

输出格式:

如果存在某种情况与小 K 的记忆吻合,输出“Yes”,否则输出“No”。

输入输出样例
输入样例#1: 

3 3
3 1 2
1 1 3 1
2 2 3 2

输出样例#1: 
Yes

说明

对于 100% 的数据保证:1 ≤ n,m,a,b,c ≤ 10000。

Differential constraints can be solved shortest and longest road. This question is also the differential template title constraint.
according to:

 农场a比农场b至少多种植了c个单位的作物,
    农场a比农场b至多多种植了c个单位的作物,
    农场a与农场b种植的作物数一样多。

Established conditions.
Seen from the title, at least a plurality farm crops planted c farm units than b, so Xa-Xb>=ci.e. Xb-Xa<=-c, a ratio of the farm to farm b c a lot of crops planted units, therefore Xa-Xb<=c, a farm as the number of the farm crops planted b more so Xa==Xb, then Xa-Xb<=c, Xb-Xa<=c.
Then SPFA. (Do not even wrong weight)

#include<bits/stdc++.h>
using namespace std;

const int maxn=11000;
const int inf=0x3f3f3f3f;

int n,m;

struct node{
    int v,w;
    node(){ }
    node(int _v,int _w){
        v=_v;
        w=_w;
    }
};

vector <node> g[maxn];
int dst[maxn];
queue <int> qu;
bool inq[maxn];
int cnt[maxn];


int add(int u,int v,int w){
    g[u].push_back(node(v,w));
}

bool spfa(int u){
    memset(dst,inf,sizeof dst);
//  memset(cnt,0,sizeof cnt);
    dst[u]=0;
    qu.push(u);
    inq[u]=1;
    while(!qu.empty()){
        u=qu.front();
        qu.pop();
        inq[u]=0;
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].v;
            int w=g[u][i].w;
            if(dst[v]>dst[u]+w){
                dst[v]=dst[u]+w;
                if(!inq[v]){
                    qu.push(v);
                    inq[v]=1;
                    cnt[v]++;
                    if(cnt[v]>n){
                        return 0;   
                    }
                }
            }
        }
    }
    return 1;
}


int main(){
    cin >> n >> m;
     for(int i=1;i<=n;i++){
        add(0,i,0);
    }
    for(int i=0;i<m;i++){
        int d,a,b,c;
        cin >> d;
        if(d==1){
            cin >>a>>b>>c;
            g[a].push_back(node(b,-c));
        }else if(d==2){
            cin >>a>>b>>c;
            g[b].push_back(node(a,c));
        }else{
            cin >>a>>b;
            g[a].push_back(node(b,0));
            g[b].push_back(node(a,0));
        }
    }
    
   
    if(spfa(0)){
        cout << "Yes";
    }else{
        cout << "No";
    }
    return 0;
}

But in fact, it can only get 60 points. Four groups of TLE. . .
Thus, it relates to a data structure to another, bidirectional queue. There are two-way nature of the queue queue and stack. From both ends into the team, pop. In this question, we determined if used, a large value in the queue into the rear end, the front end of each access into a smaller value and the last element of the first element with the back and front.

#include<bits/stdc++.h>
using namespace std;

const int maxn=11000;
const int inf=0x3f3f3f3f;

int n,m;

struct node{
    int v,w;
    node(){ }
    node(int _v,int _w){
        v=_v;
        w=_w;
    }
};

vector <node> g[maxn];
int dst[maxn];
deque<int> qu;//双向队列 
bool inq[maxn];
int cnt[maxn];


int add(int u,int v,int w){
    g[u].push_back(node(v,w));
}

bool spfa(int u){
    memset(dst,inf,sizeof dst);//初始化 
//  memset(cnt,0,sizeof cnt);
    dst[u]=0;
    qu.push_back(u);//双向队列的访问最后一个元素写法 
    inq[u]=1;
    while(!qu.empty()){
        u=qu.front();//双向队列的访问第一个元素写法 
        qu.pop_front();
        if(dst[qu.front()]>dst[qu.back()]){
            swap(qu.front(),qu.back());//交换
        }
        inq[u]=0;
        for(int i=0;i<g[u].size();i++){
            int v=g[u][i].v;
            int w=g[u][i].w;
            if(dst[v]>dst[u]+w){
                dst[v]=dst[u]+w;
                if(!inq[v]){
                    if(dst[v]<dst[qu.front()]){//判断,比较大小 
                        qu.push_front(v);//插入队首 
                    }else{
                        qu.push_back(v);//插入队尾 
                    }
                    inq[v]=1;
                    cnt[v]++;
                    if(cnt[v]>n){
                        return 0;   
                    }
                }
            }
        }
    }
    return 1;
}


int main(){
    cin >> n >> m;
     for(int i=1;i<=n;i++){
        add(0,i,0);
    }
    for(int i=0;i<m;i++){
        int d,a,b,c;
        cin >> d;
        if(d==1){//连权值 
            cin >>a>>b>>c;
            g[a].push_back(node(b,-c));
        }else if(d==2){
            cin >>a>>b>>c;
            g[b].push_back(node(a,c));
        }else{
            cin >>a>>b;
            g[a].push_back(node(b,0));
            g[b].push_back(node(a,0));
        }
    }
    
   
    if(spfa(0)){
        cout << "Yes";
    }else{
        cout << "No";
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/A-Konnyaku/p/10993063.html