【codevs 4246】奶牛的身高 差分约束

用差分约束乱搞吧……
在那次月赛里算个水题了……
点击就送屠龙宝刀

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 100000 + 5;
const int MAXM = 100000 + 5;
struct edge
{
    int f,t,v;
}l[MAXM * 2];
int first[MAXN],next[MAXN * 2],tot;
int dis[MAXN];
bool use[MAXN];
bool in[MAXN];
void init()
{
    memset(first,0xfff,sizeof(first));
    memset(use,0,sizeof(use));
    memset(dis,0,sizeof(dis));
    memset(in,0,sizeof(in));
    tot = 0;
    return;
}
void build(int f,int t,int v)
{
    l[++tot] = (edge){f,t,v};
    next[tot] = first[f];
    first[f] = tot;
    return;
}
queue <int> q;
bool bfs(int u)
{
    while(!q.empty())
        q.pop();
    q.push(u);
    use[u] = true;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        for(int i = first[u];i != -1;i = next[i])
        {
            int v = l[i].t;
            if(use[v])
                if(dis[v] != dis[u] + l[i].v)
                    return false;
            dis[v] = dis[u] + l[i].v;
            if(!use[v])
                q.push(v);
            use[v] = true;
        }
    }
    return true;
}
int T,n,m,f,t,v;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= m;i ++)
        {
            scanf("%d %d %d",&t,&f,&v);
            build(f,t, v);
            build(t,f,-v);
        }
        for(int i = 1;i <= n;i ++)
            if(!bfs(i))
            {
                puts("Bessie is blind.");
                goto cont;
            }
        puts("Bessie's eyes are good");
        cont:
            continue;
    }
    return 0;
}

你可能感兴趣的:(差分约束,最短路,=====图论=====)