結果

問題 No.654 Air E869120
ユーザー koprickykopricky
提出日時 2018-04-24 22:35:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 4,148 bytes
コンパイル時間 3,473 ms
コンパイル使用メモリ 166,100 KB
実行使用メモリ 6,352 KB
最終ジャッジ日時 2023-09-10 00:01:22
合計ジャッジ時間 3,701 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 11 ms
4,376 KB
testcase_11 AC 9 ms
4,380 KB
testcase_12 AC 9 ms
4,380 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 8 ms
4,380 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 6 ms
4,380 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 4 ms
6,352 KB
testcase_31 AC 3 ms
6,180 KB
testcase_32 AC 4 ms
6,160 KB
testcase_33 AC 4 ms
6,064 KB
testcase_34 AC 3 ms
6,068 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define INF (1LL << 60)
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 1005;

template<typename T> class Dinic {
public:
    struct edge{
        int to;
        T cap;
        int rev;
    };
    vector<vector<edge> > G;
    vector<int> level,iter;
    int n;
    Dinic(int node_size){
        n = node_size;
        G.resize(node_size),level.resize(node_size),iter.resize(node_size);
    }
    //辺を張る
    void add_edge(int from,int to,T cap)
    {
    	G[from].push_back((edge){to,cap,(int)G[to].size()});
    	G[to].push_back((edge){from,(T)0,(int)G[from].size()-1});
    }
    void bfs(int s)
    {
    	fill(level.begin(),level.end(),-1);
    	queue<int> que;
    	level[s] = 0;
    	que.push(s);
    	while(!que.empty()){
    		int v = que.front();
    		que.pop();
    		rep(i,G[v].size()){
    			edge &e = G[v][i];
    			if(e.cap > 0 && level[e.to] < 0){
    				level[e.to] = level[v] + 1;
    				que.push(e.to);
    			}
    		}
    	}
    }
    T dfs(int v,int t,T f)
    {
    	if(v==t){
    		return f;
    	}
    	for(int &i = iter[v];i<(int)G[v].size();i++){
    		edge &e = G[v][i];
    		if(e.cap > 0 && level[v] < level[e.to]){
    			T d = dfs(e.to,t,min(f,e.cap));
    			if(d>0){
    				e.cap -= d;
    				G[e.to][e.rev].cap += d;
    				return d;
    			}
    		}
    	}
    	return 0;
    }
    //最大流を計算
    T max_flow(int s,int t)
    {
    	T flow = 0;
    	for(;;){
    		bfs(s);
    		if(level[t]<0){
    			return flow;
    		}
    		fill(iter.begin(),iter.end(),0);
    		T f;
    		while((f=dfs(s,t,numeric_limits<T>::max())) > 0){
    			flow += f;
    		}
    	}
    }
};

struct data
{
    int from,to,st,ed,cap;
};

vector<P> vec[MAX_N];
int id[MAX_N][MAX_N];
int st[MAX_N];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m,d;
    cin >> n >> m >> d;
    vector<data> dat(m);
    Dinic<ll> dn(2*m+2);
    rep(i,m){
        int a,b,c,d,e;
        cin >> a >> b >> c >> d >> e;
        dat[i] = (data){a-1,b-1,c,d,e};
        vec[a-1].pb(P(c,i));
    }
    int cnt = 0;
    rep(i,n){
        if(len(vec[i])){
            sort(all(vec[i]));
            rep(j,len(vec[i])){
                id[i][j] = cnt;
                st[vec[i][j].se] = cnt;
                if(j < len(vec[i])-1){
                    dn.add_edge(cnt,cnt+1,INF);
                }
                cnt++;
            }
        }
    }
    dn.add_edge(2*m,id[0][0],INF);
    rep(i,m){
        dn.add_edge(st[i],cnt,dat[i].cap);
        if(dat[i].to == n-1){
            dn.add_edge(cnt,2*m+1,INF);
        }else{
            auto& v = vec[dat[i].to];
            int num = lower_bound(all(v),P(dat[i].ed+d,-1)) - v.begin();
            if(num < len(v)){
                dn.add_edge(cnt,id[dat[i].to][num],INF);
            }
        }
        cnt++;
    }
    cout << dn.max_flow(2*m,2*m+1) << "\n";
    return 0;
}
0