SXCPC2018 nucoj1999 占领城市

pic

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, e, a[305], w[305][305], uu, vv, ww, hea[805], cnt, ss, tt, maxFlow;
int lev[805], cur[805];
const int oo=0x3f3f3f3f;
queue<int> d;
struct Edge{
	int too, nxt, val;
}edge[200005];
void add_edge(int fro, int too, int val){
	edge[cnt].nxt = hea[fro];
	edge[cnt].too = too;
	edge[cnt].val = val;
	hea[fro] = cnt++;
}
void addEdge(int fro, int too, int val){
	add_edge(fro, too, val);
	add_edge(too, fro, 0);
}
bool bfs(){
	memset(lev, 0, sizeof(lev));
	lev[ss] = 1;
	d.push(ss);
	while(!d.empty()){
		int x=d.front();
		d.pop();
		for(int i=hea[x]; i!=-1; i=edge[i].nxt){
			int t=edge[i].too;
			if(!lev[t] && edge[i].val>0){
				lev[t] = lev[x] + 1;
				d.push(t);
			}
		}
	}
	return lev[tt]!=0;
}
int dfs(int x, int lim){
    if(x==tt)   return lim;
    int addFlow=0;
    for(int &i=cur[x]; i!=-1; i=edge[i].nxt){
        int t=edge[i].too;
        if(lev[t]==lev[x]+1 && edge[i].val>0){
            int tmp=dfs(t, min(lim-addFlow, edge[i].val));
            edge[i].val -= tmp;
            edge[i^1].val += tmp;
            addFlow += tmp;
            if(addFlow==lim)    break;
        }
    }
    return addFlow;
}
void dinic(){
	while(bfs()){
		for(int i=ss; i<=tt; i++)
			cur[i] = hea[i];
		maxFlow += dfs(ss, oo);
	}
}
bool chk(int lim){
	memset(hea, -1, sizeof(hea));
	ss = 0; tt = n + m + 1;
	cnt = 0;
	for(int i=1; i<=n; i++){
		addEdge(ss, i, 1);
		for(int j=1; j<=m; j++)
			if(w[a[i]][j]<=lim)
				addEdge(i, n+j, 1);
	}
	for(int i=1; i<=m; i++)
		addEdge(n+i, tt, 1);
	maxFlow = 0;
	dinic();
	if(maxFlow!=min(n,m))	return false;
	return true;
}
int main(){
	cin>>n>>m>>e;
	memset(w, 0x3f, sizeof(w));
	for(int i=1; i<=n; i++)
		scanf("%d", &a[i]);
	for(int i=1; i<=m; i++)
		w[i][i] = 0;
	for(int i=1; i<=e; i++){
		scanf("%d %d %d", &uu, &vv, &ww);
		w[uu][vv] = w[vv][uu] = min(w[uu][vv], ww);
	}
	for(int k=1; k<=m; k++)
		for(int i=1; i<=m; i++)
			for(int j=1; j<=m; j++)
				w[i][j] = min(w[i][j], w[i][k]+w[k][j]);
	int l=0, r=258262, mid, re;
	while(l<=r){
		mid = (l + r) >> 1;
		if(chk(mid))	re = mid, r = mid - 1;
		else	l = mid + 1;
	}
	cout<<re<<endl;
	return 0;
}
posted @ 2018-05-07 19:25  poorpool  阅读(151)  评论(0编辑  收藏  举报