HDU 5294 Tricks Device (2015 Multi-school first game 7th) Max Stream + shortest circuit

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5294


Test Instructions: give you n a tomb, m path, a person in the Tomb No. 1th (beginning), another person in n

Tomb (end), the beginning of the person only through the shortest path to catch up with the end of the person, and the end of the person can cut off any path.

The first question--the end of the person to make the starting point that the person can not catch up the number of paths to be cut, the minimum number of paths to output

The second question--the beginning that man can catch up with the end of the person's case, the end that person can cut the maximum number of paths, output the maximum number of paths


Ideas: to make the starting point that the person can not catch up, as long as the shortest path does not exist on the good, so long as the shortest path to the flow of each road is 1, and the maximum flow of the ball out 1-n is, can cut the least number of paths;

If the beginning of the person can catch up, so long as his shortest path exists, and in these shortest path to find the least number of paths, then the maximum number of paths is the least M-path number of the path number.


Code:

#include  <cstdio> #include  <cstdlib> #include  <cmath> #include  <cstring > #include  <iostream> #include  <queue> #include  <algorithm> #include  < vector>using namespace std; #define  ll __int64#define inf 0x3f3f3f3fconst  int maxn=20005;struct qnode{int v;int c;qnode (int _v=0,int _c=0): V (_v), C (_ c) {}bool operator < (Const qnode &r) const{return c>r.c;}}; Bool vis[2005];int dist[2005];int mp[2005][2005];void dijkstra (Int n,int start)/ /Find the shortest path size {memset (vis,false,sizeof (VIS)); for (int i=1;i<=n;i++) Dist[i]=inf;priority_queue<qnode>que , while (!que.empty ()) Que.pop ();d Ist[start]=0;que.push (Qnode (start,0,0)); Qnode tmp;while (!que.empty ()) {tmp= Que.top (); Que.pop (); Int u=tmp.v;if (Vis[u]) continue;vis[u]=true;for (int i=1;i<=n;i++)           {            if (!vis[i]&&mp[u][i]<inf &&dist[i]>dist[u]+mp[u][i])              {                dist[i]=dist[ u]+mp[u][i];                 Que.push (Qnode (i,dist[i));            }  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}}}INT&NBSP;MP2[2005][2005];VOID&NBSP;DIJKSTRA1 (int n,int  Start)//Find the shortest possible route {memset (vis,false,sizeof (VIS)); for (int i=1;i<=n;i++) Dist[i]=inf;priority_queue<qnode >que;while (!que.empty ()) Que.pop ();d Ist[start]=0;que.push (Qnode (start,0,0)); Qnode tmp;while (!que.empty ( ) {tmp=que.top (); Que.pop (); Int u=tmp.v;int gg=tmp.flag+1;if (Vis[u]) continue;vis[u]=true;for (int  i=1;i<=n;i++)         {             if (!vis[i]&&mp2[u][i]<inf&&dist[i]>dist[u]+mp2[u][i])              {                 dist[i]=dist[u]+mp2[u][i];                 que.push (Qnode (i,dist[i));             }        }}}struct  edge{    int from,to,cap,flow;    edge () {}     edge (INT&NBSP;F,INT&NBSP;T,INT&NBSP;C,INT&NBSP;FL): From (f), to (t), Cap (c), Flow (fl) {}};struct  dinic{    int n,m,s,t;    vector<edge> edges;     vector<int>&nBsp g[maxn];    int cur[maxn];    int d[maxn];     bool vis[maxn];    void init (int n,int s,int t)      {        this->n=n, this->s=s, this->t= T;        edges.clear ();         for (int i=0;i<n;i++)  g[i].clear ();     }    void  addedge (INT&NBSP;FROM,INT&NBSP;TO,INT&NBSP;CAP)     {         edges.push_back ( edge (from,to,cap,0)  );         edges.push_back ( edge (to,from,0,0)  )         m =  edges.size ();         g[from].push_back (m-2);       &nBsp; g[to].push_back (m-1);     }    bool bfs ()      {        queue<int> Q;         q.push (s);         memset (vis,0,sizeof (VIS));         d[s]=0;        vis[s]= True;        while (! Q.empty ())         {             int x=q.front ();  q.pop ();             for (Int i=0;i<g[x].size (); ++i)              {                 edge& e=edges[g[x][i]];                 if (!vis[e.to] && e.cap >e.flow)                 {                      d[e.to]=1+d[x];                     vis[e.to]=true;                     q.push (e.to);                 }             }        }         return vis[t];    }    int dfs (int  x,int a)     {        if (x==t | |  a==0)  return a;        int flow=0,f;         for (Int& i=cur[x];i<g[x].size (); ++i)          {            edge&  e=edges[g[x][i]];            if (d[e.to]==d[x ]+1 &&  (F=dfs (E.to,min (a,e.cap-e.flow)  )  ) >0)              {                 e.flow +=f;                 edges[G[x][i]^1].flow -=f;                 flow +=f;                a-=f;                 if (a==0)   break;            }         }        return flow;    }     int max_flow ()     {         int ans=0;        while (BFS ())          {            memset (cur,0, sizeof (cur));             ans += dfs (s), INF);         }        return  ans;  &Nbsp; }}dc;int ggg[2005][2005];int main () {    int n,m,i,j,a,b,c;     while (~scanf ("%d%d", &n,&m))     {         dc.init (n,1,n);         memset (Ggg,0,sizeof (GGG));         for (i=1;i<=n;i++)              for (j=1;j<=n;j++)                  mp[i][j]=mp2[i][j]=inf;        for (i=0;i<m;i++)         {        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d%d%d", &a,&b,&c);             if (mp[a][b]>c)              {                mp[a][b]=c;                 mp[b][a]=c;                 ggg[a][b]=1;                 ggg[b][a]=1;             }             else if (mp[a][b]==c)              {                 ggg[a][b]++;                 ggg[b][a]++;            }         }         dijkstra (n,1);         for (i=1;i<=n;i++)         {             for (j=1;j<=n;j++)              {                 if (Mp[i][j]+dist[i]==dist[j])                  {                     for (int k=0;k<ggg[i][j];k++)                          dc . Addedge (i,j,1);                     mp2[i][j]=1;                 }            }         }        dijkstra1 (n,1);         printf ("%d %d\n", Dc.max_flow (), m-dist[n]);    }     return 0;}


This article is from the "Acfun" blog, make sure to keep this source http://9712315.blog.51cto.com/9702315/1677012

HDU 5294 Tricks Device (2015 Multi-school first game 7th) Max Stream + shortest circuit

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.