Bzoj3931 [CQOI2015] network throughput
3931: [CQOI2015] network throughput Description
Routing refers to the activity of transmitting information from the source address to the destination address through the computer network, which is also the key and difficult point in the computer network design. The hardware device that implements route forwarding in the network is called a router. In order for the data packet to reach the destination as quickly as possible, the router needs to select the optimal path to forward the data packet. For example, in the common routing algorithm OSPF (Open Shortest Path First), the router uses the classic Dijkstra algorithm to calculate the shortest path, and then tries to forward data packets along the shortest path. Now, if you know the connection between routers in a computer network and the maximum throughput of Each router (that is, the number of data packets that can be forwarded per second), assume that all data packets must be forwarded along the shortest path, calculate the maximum network throughput from vro1 1 to Vron n. In computing, the time overhead of forwarding and transmission is ignored, regardless of the bandwidth limit of the link. That is, packets can be instantly transmitted over the network. Vro1 1 to Vron n serves as the start point and end point. You do not need to consider the throughput of your vro1. There is no link directly connecting vro1 1 and vro1 n on the network.
Input
The first line of the input file contains two spaces separated positive integers n and m, indicating the number of routers and the number of links, respectively. Vrouters in the network use numbers 1 to n. In the next m line, each line contains three spaces separated positive integers a, B, and d, indicating that there is a two-way link between router a and router B with a distance of d. In the next n rows, each line contains a positive integer c, indicating the throughput of Each router.
Output
Output an integer for the throughput.
Sample Input7 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 Output70HINT
For 100% of data, n ≤ 500, m ≤ 100000, d, c ≤ 10 ^ 9
Source
Shortest Path + Shortest Path
#include
#include
#include
#include
#include
#include#include
#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define ll long long#define pa pair
#define maxn 1100#define maxm 400100#define inf 1000000000000000llusing namespace std;int n,m,s,t,cnt=0;int head[maxn],cur[maxn],x[100100],y[100100];ll dis[maxn],c[maxn],z[100100];ll ans=0;bool inq[maxn],vst[maxn];struct edge_type{int to,next;ll v;}e[maxm];inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}inline void add_edge(int x,int y,ll z1,ll z2){e[++cnt]=(edge_type){y,head[x],z1};head[x]=cnt;e[++cnt]=(edge_type){x,head[y],z2};head[y]=cnt;}inline void dijkstra(){priority_queue
,greater
> q;memset(dis,-1,sizeof(dis));dis[1]=0;q.push(make_pair(0,1));while (!q.empty()){int x=q.top().second;q.pop();while (!q.empty()&&vst[x]){x=q.top().second;q.pop();}if (vst[x]) break;vst[x]=true;for(int i=head[x];i;i=e[i].next){int y=e[i].to;if (dis[y]==-1||dis[y]>dis[x]+e[i].v){dis[y]=dis[x]+e[i].v;q.push(make_pair(dis[y],y));}}}}inline ll dfs(int x,ll f){ll tmp,sum=0;if (x==t) return f;for(int &i=cur[x];i;i=e[i].next){int y=e[i].to;if (e[i].v&&dis[y]==dis[x]+1){tmp=dfs(y,min(f-sum,e[i].v));e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;if (sum==f) return sum;}}if (!sum) dis[x]=-1;return sum;}inline bool bfs(){queue
q;memset(dis,-1,sizeof(dis));dis[s]=0;q.push(s);while (!q.empty()){int tmp=q.front();q.pop();if (tmp==t) return true;for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1){dis[e[i].to]=dis[tmp]+1;q.push(e[i].to);}}return false;}inline void dinic(){while (bfs()){F(i,1,t) cur[i]=head[i];ans+=dfs(s,inf);}}int main(){n=read();m=read();F(i,1,m){x[i]=read();y[i]=read();z[i]=read();add_edge(x[i],y[i],z[i],z[i]);}F(i,1,n) c[i]=read();c[1]=c[n]=inf;dijkstra();memset(head,0,sizeof(head));cnt=1;s=1;t=2*n;F(i,1,n) add_edge(i,i+n,c[i],0);F(i,1,m){if (dis[y[i]]==dis[x[i]]+z[i]) add_edge(x[i]+n,y[i],inf,0);if (dis[x[i]]==dis[y[i]]+z[i]) add_edge(y[i]+n,x[i],inf,0);}dinic();printf("%lld\n",ans);}