标签:style http os io strong for ar html amp
题目链接: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 <cstring> #include <queue> #include <stack> #include <algorithm> const int N = 210; const int maxn = 1010; const int maxm = 21000; #define FOR(i,a,b) for(int i=a;i<b;i++) #define init(a) memset(a,0,sizeof(a)) #define MIN INT_MIN #define MAX INT_MAX #define LL long long using namespace std; const int INF=0x3f3f3f3f; int dis[maxm],cnt,n; struct node { int u,v,w; } edge[maxm]; void add(int u,int v,int w) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; cnt++; } int Bellman(int s,int t) { int flag; FOR(i,0,t+1) { dis[i]=INF; } dis[s]=0; FOR(i,0,n) { flag=0; FOR(j,0,cnt) { if(dis[edge[j].v] > dis[edge[j].u] + edge[j].w) { dis[edge[j].v] = dis[edge[j].u] + edge[j].w; flag = 1; } } if(!flag) break; } if(flag==1) return -1; else if(dis[t]==INF) return -2; else return dis[t]; } int main() { int u,v,w,a,b; while(scanf("%d%d%d",&n,&a,&b)!=EOF) { cnt=0; FOR(i,0,a) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); } FOR(i,0,b) { scanf("%d%d%d",&u,&v,&w); add(v,u,-w); } int ans = Bellman(1,n); cout<<ans<<endl; } return 0; }
poj 3169 Layout (差分约束+Bellman )
标签:style http os io strong for ar html amp
原文地址:http://blog.csdn.net/wjw0130/article/details/38781277