3931: [CQOI2015] Network throughput "Network Flow"

Source: Internet
Author: User

Network throughput
(Network)
Title Description
Routing refers to the activity of transmitting information from source address to destination site through computer network, which is also the key point and difficulty in computer network design. Implemented in the network
The hardware device for routing forwarding is called a router. In order for the packet to reach the destination fastest, the router needs to select the optimal path forward packet. For example
In the Common routing algorithm OSPF (Open Shortest Path first), routers use the classic Dijkstra algorithm to calculate the shortest path, and then try to follow the shortest
The path forwards the packet.
Now, if you know the connection between routers in a computer network, and the maximum throughput of each router (that is, the number of packets that can be forwarded per second
, assuming that all packets must be forwarded along the shortest path, try to calculate the maximum throughput of the network from Router 1 to Router n. Ignoring forwarding and transmitting in the calculation
The time overhead of the loss, regardless of the bandwidth limit of the link, is that the packet can be instantaneous over the network. Router 1 to Router n as the start and end point, itself
Throughput, there is no link on the network that connects 1 and N directly.
Input format
The first line of the input file contains two spaces separated by positive integers n and m, each representing the number of routers and the number of links. Routers in the network use 1 to N series
No.
Next m lines, each line contains three spaces separated by a positive integer A, B, and D, indicating that there is a two-way link from router A to Router B with a distance of D.
Next n rows, each line contains a positive integer c, each of which gives the throughput of each router.
Output format
Output an integer for the throughput of the topic.
Input sample
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
Output sample
70

Idea: First run the shortest circuit, and then obviously the network flow on the edge must be on the side, and then the limit is on the point, so need to split, run side Maxflow can

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <queue>5 #defineMAXN 4000096 #defineInf ((1ll<<63)-1)7 #definell Long Long8 using namespacestd;9 ll HEAD[MAXN],NEXT[MAXN],POINT[MAXN],NOW,P[MAXN],FLOW[MAXN];Ten ll N,M,VALUE[MAXN],DIST[MAXN],DIS[MAXN],X[MAXN],Y[MAXN],V[MAXN]; One voidAdd (ll x,ll Y,ll v) A { -next[++now]=Head[x]; -head[x]=Now ; thepoint[now]=y; -value[now]=v; - } - voidadd2 (ll x,ll Y,ll v) + { -next[++now]=Head[x]; +head[x]=Now ; Apoint[now]=y; atflow[now]=v; -next[++now]=Head[y]; -head[y]=Now ; -point[now]=x; -flow[now]=0; - } in voidSPFA (ll s) - { tomemset (dist,-1,sizeof(Dist)); +     intvisit[maxn]={0}; -visit[s]=1; theQueue<ll>Q; * Q.push (s); $dist[s]=0;Panax Notoginseng      while(!q.empty ()) -     { the         intu=Q.front (); + Q.pop (); Avisit[u]=0; the          for(intI=head[u];i;i=Next[i]) +         { -ll k=Point[i]; $             if(dist[k]==-1|| dist[u]+value[i]<Dist[k]) $             { -dist[k]=dist[u]+Value[i]; -p[k]=u; the                 if(!Visit[k]) -                 {Wuyivisit[k]=1; the Q.push (k); -                 } Wu             } -         } About     } $ } - intBFS (ll s,ll t) - { -Queue<ll>Q; Amemset (dis,-1,sizeof(DIS)); + Q.push (s); thedis[s]=0; -      while(!q.empty ()) $     { thell u=Q.front (); the Q.pop (); the          for(intI=head[u];i;i=Next[i]) the         { -ll k=Point[i]; in             if(dis[k]==-1&&Flow[i]) the             { thedis[k]=dis[u]+1; About Q.push (k); the             } the         } the     } +     returndis[t]!=-1; - } the ll Dfs (ll s,ll d,ll t)Bayi { the     if(s==t)returnD; thell res=0; -      for(intI=head[s];i&&res<d;i=Next[i]) -     { thell u=Point[i]; the         if(Flow[i] && dis[u]==dis[s]+1) the         { thell Dd=dfs (U,min (flow[i],d-res), T); -             if(DD) the             { theflow[i]-=DD; theflow[((I-1)^1)+1]+=DD;94res+=DD; the             } the         } the     }98     if(res==0) dis[s]=-1; About     returnRes; - }101 intMain ()102 {103scanf"%lld%lld",&n,&m);104          for(intI=1; i<=m;i++) the         {106scanf"%lld%lld%lld",&x[i],&y[i],&v[i]);107 Add (X[i],y[i],v[i]);108 Add (Y[i],x[i],v[i]);109         } theSPFA (1);111Memset (Head,0,sizeof(head)); now=0; the      for(intI=1; i<=m;i++)113     { the         if(Dist[x[i]] + v[i] = =Dist[y[i]]) the         { theADD2 (x[i]+n,y[i],inf);117         }118         if(Dist[y[i]] + v[i] = =Dist[x[i]])119         { -ADD2 (y[i]+n,x[i],inf);121         }122     }123 ll B;124          for(intI=1; i<=n;i++) the     {126scanf"%lld",&b);127ADD2 (I,i + N, (i==1|| i==n)?inf:b); -     }129ll ans=0; the      while(BFS (1, n +N))131     { theAns + = DFS (1, INF, n +n);133     }134printf"%lld\n", ans);135         return 0;136}

3931: [CQOI2015] Network throughput "Network Flow"

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.