bzoj-3931 Network throughput

Source: Internet
Author: User

Test instructions

A graph of n nodes is given, and each node has a throughput;

The rule is that the packet delivery must follow the shortest path, and the network throughput of 1 to n is obtained;


Exercises
As Huang said, the problem is test instructions;

But there are a few things to be aware of;

The shortest circuit is dij or SPFA, but for the purpose of building a map, you need to record the path;

If the chain forward star can be all used to update the side to save the map;

But using vectors or adjacency matrices (not knowing if they can live) can not be achieved;

I use a vector array to hang the chain above;

Topology queue sweep over plus side, the complexity is quite must not;

Then the demolition of the map is nothing to say;

The shortest circuit and flow are to open long long, the throughput of the first and last point is not counted;



Code:


#include <queue> #include <vector> #include <stdio.h> #include <string.h> #include < Algorithm> #define PR pair<int,int> #define N 600#define M 210000using namespace Std;typedef long Long ll;queue&lt ; INT&GT;Q;PRIORITY_QUEUE&LT;PR,VECTOR&LT;PR &GT;,GREATER&LT;PR > >poq;vector<int>to[n],pre[n];vector <ll>val[n];int head[n<<1],next[m],t[m],n,tot=1;ll dis[n<<1],flow[m];bool vis[N];void Add (int x, int y,ll fl) {T[++TOT]=Y,NEXT[TOT]=HEAD[X],HEAD[X]=TOT,FLOW[TOT]=FL; t[++tot]=x,next[tot]=head[y],head[y]=tot,flow[tot]=0;} void Dij () {memset (dis,0x3f,sizeof (dis));d is[1]=0;poq.push (pr (0,1)); int X,y,i;while (!poq.empty ()) {x=poq.top (). Second,poq.pop (); if (Vis[x]) continue;vis[x]=1;for (I=0;i<to[x].size (); i++) {if (!vis[y=to[x][i]]) {if (dis[y]> Dis[x]+val[x][i]) {Dis[y]=dis[x]+val[x][i];p oq.push (pr (dis[y],y));p re[y].clear ();p re[y].push_back (x); else if (Dis[y]==dis[x]+val[x][i]) pre[y].push_back (x);}}} BOOL BFs () {memset (dis,-1,sizeof (dis));d Is[3]=0;q.push (3); int x,y,i;while (!q.empty ()) {X=q.front (), Q.pop (); for (I=head[x];i;i=next[i]) {if (flow[i]>0&&dis[y=t[ i]]<0) {Dis[y]=dis[x]+1;q.push (y);}}} return dis[n<<1]>=0;} ll Dfs (int x,ll k) {if (x== (n<<1)) return k;int y,i;ll j,ret=0;for (I=head[x];i;i=next[i]) {if (flow[i]>0& &dis[y=t[i]]==dis[x]+1) {J=dfs (Y,min (Flow[i],k-ret)); Flow[i]-=j,flow[i^1]+=j;ret+=j;if (K==ret) return ret;}} return ret;} int main () {int m,i,j,k,x,y;ll v,ans;scanf ("%d%d", &n,&m), for (i=1;i<=m;i++) {scanf ("%d%d%lld", &x, &AMP;Y,&AMP;V); To[x].push_back (y), Val[x].push_back (v); To[y].push_back (x), Val[y].push_back (v);} Dij (); Q.push (n); memset (vis,0,sizeof (VIS)), while (!q.empty ()) {X=q.front (), Q.pop (); for (I=0;i<pre[x].size (); i++ {Y=pre[x][i];add (Y&LT;&LT;1|1,X&LT;&LT;1,0X3F3F3F3F3F3F3F3FLL), if (!vis[y]) Vis[y]=1,q.push (y);}} for (i=1;i<=n;i++) scanf ("%lld", &v), add (i<<1,i<<1|1,v), Ans=0;while (BFS ()) Ans+=dfs ( 3,0X3F3F3F3F3F3F3F3FLL);p rintf ("%lld", ans); return 0;}


bzoj-3931 Network throughput

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.