3931: [CQOI2015]网络吞吐量

3931: [CQOI2015]网络吞吐量

Time Limit: 10 Sec   Memory Limit: 512 MB
Submit: 1536   Solved: 628
[ Submit][ Status][ Discuss]

Description

 路由是指通过计算机网络把信息从源地址传输到目的地址的活动,也是计算机网络设计中的重点和难点。网络中实现路由转发的硬件设备称为路由器。为了使数据包最快的到达目的地,路由器需要选择最优的路径转发数据包。例如在常用的路由算法OSPF(开放式最短路径优先)中,路由器会使用经典的Dijkstra算法计算最短路径,然后尽量沿最短路径转发数据包。现在,若已知一个计算机网络中各路由器间的连接情况,以及各个路由器的最大吞吐量(即每秒能转发的数据包数量),假设所有数据包一定沿最短路径转发,试计算从路由器1到路由器n的网络的最大吞吐量。计算中忽略转发及传输的时间开销,不考虑链路的带宽限制,即认为数据包可以瞬间通过网络。路由器1到路由器n作为起点和终点,自身的吞吐量不用考虑,网络上也不存在将1和n直接相连的链路。

Input

输入文件第一行包含两个空格分开的正整数n和m,分别表示路由器数量和链路的数量。网络中的路由器使用1到n编号。接下来m行,每行包含三个空格分开的正整数a、b和d,表示从路由器a到路由器b存在一条距离为d的双向链路。 接下来n行,每行包含一个正整数c,分别给出每一个路由器的吞吐量。

Output

输出一个整数,为题目所求吞吐量。

Sample Input

7 10
1 2 2
1 5 2
2 4 1
2 3 3
3 7 1
4 5 4
4 3 1
4 6 1
5 6 2
6 7 1
1
100
20
50
20
60
1

Sample Output

70

HINT

 对于100%的数据,n≤500,m≤100000,d,c≤10^9

Source

[ Submit][ Status][ Discuss]

运输最多物品显然是个最大流啊,每个点拆成两个点,中间连一条容量为该点容量的边就是了
原图中的边只保留最短路图上的边
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 1010;
const int maxm = 4E5 + 20;
typedef long long LL;
const LL INF = 1E16;
const int inf = ~0U>>1;

struct E{
	int to,cap,flow; E(){}
	E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];

struct data{
	int to,w; data(){}
	data(int to,int w): to(to),w(w){}
};

int n,m,tot,cnt,S,T,A[maxn],B[maxn],L[maxn],cur[maxn];
LL dis[maxn];
bool vis[maxn];

vector <int> v[maxn];
vector <data> G[maxn];
queue <int> Q;

void Add(int x,int y,int cap)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}

bool BFS()
{
	for (int i = S; i <= T; i++) L[i] = 0;
	L[S] = vis[S] = 1; Q.push(S);
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++)
		{
			E e = edgs[v[k][i]];
			if (e.cap == e.flow || L[e.to]) continue;
			L[e.to] = L[k] + 1; Q.push(e.to);
		}
	}
	return L[T];
}

LL Dinic(int x,LL a)
{
	if (x == T) return a;
	LL flow = 0;
	for (int &i = cur[x]; i < v[x].size(); i++)
	{
		E &e = edgs[v[x][i]];
		if (e.cap == e.flow || L[e.to] != L[x] + 1) continue;
		LL f = Dinic(e.to,min(a,1LL*(e.cap - e.flow)));
		if (!f) continue;
		flow += f; a -= f; e.flow += f;
		edgs[v[x][i]^1].flow -= f;
		if (!a) return flow;
	}
	if (!flow) L[x] = -1;
	return flow;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m;
	while (m--)
	{
		int x,y,z; scanf("%d%d%d",&x,&y,&z);
		G[x].push_back(data(y,z));
		G[y].push_back(data(x,z));
	}
	for (int i = 2; i <= n; i++) dis[i] = INF; Q.push(1);
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop(); vis[k] = 0;
		for (int i = 0; i < G[k].size(); i++)
		{
			data e = G[k][i];
			if (dis[e.to] > dis[k] + 1LL*e.w)
			{
				dis[e.to] = dis[k] + 1LL*e.w;
				if (!vis[e.to]) Q.push(e.to),vis[e.to] = 1;
			}
		}
	}
	for (int i = 2; i < n; i++) A[i] = ++tot,B[i] = ++tot; 
	B[1] = S; A[n] = T = ++tot;
	for (int i = 1; i <= n; i++)
	{
		int x; scanf("%d",&x);
		if (1 < i && i < n) Add(A[i],B[i],x);
		for (int j = 0; j < G[i].size(); j++)
		{
			data e = G[i][j];
			if (dis[e.to] == dis[i] + 1LL*e.w)
				Add(B[i],A[e.to],inf);
		}
	} 
	
	LL MaxFlow = 0;
	while (BFS())
	{
		for (int i = S; i <= T; i++) cur[i] = 0;
		MaxFlow += 1LL*Dinic(S,INF);
	}
	cout << MaxFlow;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值