比赛 20160415 评测结果 AWWWWWWWWW
题目名称 能量网络 最终得分 10
用户昵称 KZNS 运行时间 0.033 s
代码语言 C++ 内存使用 0.35 MiB
提交时间 2016-04-15 11:55:53
显示代码纯文本
//KZNS
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
//
ifstream fin ("energynet.in");
ofstream fout ("energynet.out");
const int Nmax = 1003;
//
int N, M;
int A[Nmax], B[Nmax];
vector<int> gp[Nmax];
vector<int> bmax[Nmax];
bool ex[Nmax] = {0};
int mx[Nmax][2] = {0};
class poi {
public:
	inline bool operator () (int a, int b) {
		return A[a] < A[b];
	}
};
priority_queue<int, vector<int>, poi> ls;
int u1, u2;
vector<pair<int, int> > uls;
//
void fir() {
	fin >> N >> M;
	for (int i = 1; i <= N; i++)
		fin >> A[i];
	for (int i = 1; i <= N; i++)
		fin >> B[i];
	int a, b;
	for (int i = 0; i < M; i++) {
		fin >> a >> b;
		gp[a].push_back(b);
	}
}
void ctmx(int x) {
	int a = 0, b = 0;
	for (int i = 0; i < gp[x].size(); i++) {
		if (!ex[gp[x][i]]) {
			if (A[gp[x][i]] > a) {
				a = A[gp[x][i]];
				b = gp[x][i];
			}
		}
	}
	mx[x][0] = a;
	mx[x][1] = b;
	bmax[b].push_back(x);
}
void cxxx(int x) {
	int a = 0, b = 0;
	for (int i = 0; i < gp[x].size(); i++) {
		if (!ex[gp[x][i]]) {
			if (A[gp[x][i]] > a) {
				a = A[gp[x][i]];
				b = gp[x][i];
			}
		}
	}
	u1 = a;
	u2 = b;
}
//
int main() {
	fir();
	int edd = 0;
	for (int i = 1; i <= N; i++) {
		ctmx(i);
		edd += mx[i][0];
		ls.push(i);
	}
	int u, ued;
	while (!ls.empty()) {
		uls.clear();
		u = ls.top();
		ls.pop();
		ex[u] = true;
		ued = edd;
		ued -= A[u]*bmax[u].size();
		ued += B[u];
		for (int i = 0; i < bmax[u].size(); i++) {
			cxxx(bmax[u][i]);
			ued += u1;
			uls.push_back(make_pair(u2, bmax[u][i]));
		}
		if (ued < edd) {
			edd = ued;
			for (int i = 0; i < uls.size(); i++)
				bmax[uls[i].first].push_back(uls[i].second);
		}
		else {
			ex[u] = false;
		}
	}
	fout << edd;
	return 0;
}
//UWBH