Submission #117486


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define EPS 1e-9
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;


struct CostPair {
	ll x, y;
	CostPair(): x(0), y(0) { }
	CostPair(ll x_, ll y_): x(x_), y(y_) { }
	bool operator<(const CostPair &that) {
		return mp(x,y) < mp(that.x,that.y);
	}
	bool operator==(const CostPair &that) {
		return mp(x,y) == mp(that.x,that.y);
	}
	CostPair operator-() const {
		return CostPair(-x, -y);
	}
	CostPair &operator+=(const CostPair &that) {
		x += that.x;
		y += that.y;
		return *this;
	}
};

const CostPair InfCost(INFL, INFL);
struct MinimumCostMaximumFlow {
	typedef int Index; typedef int Flow; typedef CostPair Cost;
	static const Flow InfCapacity = INF;
	struct Edge {
		Index to; Index rev;
		Flow capacity; Cost cost;
	};
	vector<vector<Edge>> g;
	void init(Index n) { g.assign(n, vector<Edge>()); }
	void add(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {
		Edge e, f; e.to = j, f.to = i; e.capacity = capacity, f.capacity = 0; e.cost = cost, f.cost = -cost;
		g[i].push_back(e); g[j].push_back(f);
		g[i].back().rev = (Index)g[j].size() - 1; g[j].back().rev = (Index)g[i].size() - 1;
	}
	void addB(Index i, Index j, Flow capacity = InfCapacity, Cost cost = Cost()) {
		add(i, j, capacity, cost);
		add(j, i, capacity, cost);
	}
	pair<Cost,Flow> minimumCostMaximumFlow(Index s, Index t, Flow f = InfCapacity) {
		int n = g.size();
		vector<Cost> dist(n); vector<Index> prev(n); vector<Index> prevEdge(n);
		pair<Cost,Flow> total = make_pair(Cost(), 0);
		while(f > 0) {
			fill(dist.begin(), dist.end(), InfCost);
			deque<Index> q;
			q.push_back(s); dist[s] = Cost(); vector<bool> inqueue(n);
			while(!q.empty()) {
				Index i = q.front(); q.pop_front(); inqueue[i] = false;
				for(Index ei = 0; ei < g[i].size(); ei ++) {
					const Edge &e = g[i][ei]; Index j = e.to; Cost d = dist[i]; d += e.cost;
					if(e.capacity > 0 && d < dist[j]) {
						if(!inqueue[j]) {
							inqueue[j] = true;
							q.push_back(j);
						}
						dist[j] = d; prev[j] = i; prevEdge[j] = ei;
					}
				}
			}
			if(dist[t] == InfCost) break;
			
			Flow d = f; Cost distt = Cost();
			for(Index v = t; v != s; ) {
				Index u = prev[v]; const Edge &e = g[u][prevEdge[v]];
				d = min(d, e.capacity); distt += e.cost; v = u;
			}
			f -= d; rep(i, d) total.first += distt; total.second += d;
			for(Index v = t; v != s; v = prev[v]) {
				Edge &e = g[prev[v]][prevEdge[v]];
				e.capacity -= d; g[e.to][e.rev].capacity += d;
			}
		}
		return total;
	}
};


int main() {
	int N, M;
	cin >> N >> M;
	map<string,pii> knightsm;
	map<string,pair<int,pii> > magesm;
	rep(i, N) {
		string name; int x, y;
		cin >> name >> x >> y;
		knightsm[name] = mp(x,y);
	}
	rep(i, M) {
		string name; int magic, x, y;
		cin >> name >> magic >> x >> y;
		magesm[name] = mp(magic,mp(x,y));
	}
	vector<pii> knights;
	vector<pair<int,pii> > mages;
	each(i,knightsm)knights.pb(i->second);
	each(i,magesm)if(i->second.first != 0)mages.pb(i->second);
	N = knights.size();
	M = mages.size();
	MinimumCostMaximumFlow mcf;
	mcf.init(N + M + 2); int s = N + M, t = s + 1;
	rep(i, N) mcf.add(s, i, 1);
	rep(i, N) rep(j, M) {
		pii p = knights[i], q = mages[j].second;
		int d = abs(p.first-q.first) + abs(p.second-q.second);
		mcf.add(i, N+j, 1, CostPair(0,d));
	}
	rep(j, M) mcf.add(N+j, t, 1, CostPair(-mages[j].first,0));
	CostPair cost = mcf.minimumCostMaximumFlow(s, t, N).first;
	cout << cost.y << endl;
//	cerr << "magic: " << cost.x << endl;
	return 0;
}

Submission Info

Submission Time
Task F - 3人の騎士と1匹の犬
User anta
Language C++11 (GCC 4.8.1)
Score 100
Code Size 4729 Byte
Status AC
Exec Time 24 ms
Memory 1056 KB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 23
Set Name Test Cases
All 00-sample, 01-minimum, 02-maximum, 50-random-00, 50-random-01, 50-random-02, 50-random-03, 50-random-04, 50-random-05, 50-random-06, 50-random-07, 50-random-08, 50-random-09, 50-random-10, 50-random-11, 50-random-12, 50-random-13, 50-random-14, 50-random-15, 50-random-16, 50-random-17, 50-random-18, 50-random-19
Case Name Status Exec Time Memory
00-sample AC 20 ms 924 KB
01-minimum AC 20 ms 800 KB
02-maximum AC 24 ms 1056 KB
50-random-00 AC 21 ms 1052 KB
50-random-01 AC 20 ms 804 KB
50-random-02 AC 23 ms 920 KB
50-random-03 AC 20 ms 924 KB
50-random-04 AC 22 ms 928 KB
50-random-05 AC 21 ms 928 KB
50-random-06 AC 22 ms 864 KB
50-random-07 AC 22 ms 748 KB
50-random-08 AC 22 ms 800 KB
50-random-09 AC 20 ms 800 KB
50-random-10 AC 21 ms 928 KB
50-random-11 AC 22 ms 932 KB
50-random-12 AC 21 ms 800 KB
50-random-13 AC 20 ms 800 KB
50-random-14 AC 21 ms 932 KB
50-random-15 AC 21 ms 928 KB
50-random-16 AC 21 ms 804 KB
50-random-17 AC 21 ms 924 KB
50-random-18 AC 20 ms 804 KB
50-random-19 AC 23 ms 1052 KB