P3116 [USACO15JAN]约会时间Meeting Time


自带拓扑图结构,对可行性求交集即可


#include<bits/stdc++.h>
using namespace std;
int f[101][11000],g[101][11000],n,m,a,b,c,d,head[1001],ne;
struct node{int nxt,to,w1,w2;}eg[100100];
void adde(int from,int to,int vl1,int vl2)
{eg[++ne].nxt=head[from];eg[ne].to=to;eg[ne].w1=vl1;eg[ne].w2=vl2;head[from]=ne;}
int main()
{
    cin>>n>>m;
    while(m--){cin>>a>>b>>c>>d;adde(a,b,c,d);}
    f[1][0]=g[1][0]=1;
    for(int i=1;i<=n;i++)
    for(int k=0;k<=10001;k++)
    {
        if(f[i][k])for(int j=head[i];j;j=eg[j].nxt)f[eg[j].to][k+eg[j].w1]=1;
        if(g[i][k])for(int j=head[i];j;j=eg[j].nxt)g[eg[j].to][k+eg[j].w2]=1;    
    }
    for(int i=0;i<=10001;i++)if(f[n][i]&&g[n][i]){cout<<i;return 0;}
    cout<<"IMPOSSIBLE";
}
原文地址:https://www.cnblogs.com/SFWR-YOU/p/11728256.html