bzoj 3931 最短路+最大流

 

较水,但因为范围问题WA了两次。。。。

 

  1 /**************************************************************
  2     Problem: 3931
  3     User: idy002
  4     Language: C++
  5     Result: Accepted
  6     Time:392 ms
  7     Memory:3224 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <vector>
 13 #include <queue>
 14 #define min(a,b) ((a)<(b)?(a):(b))
 15 #define N 1100
 16 #define M 200010
 17 #define oo 0x3f3f3f3f
 18 using namespace std;
 19  
 20 typedef long long dnt;
 21 struct Pair {
 22     int u;
 23     dnt d;
 24     Pair( int u, dnt d ):u(u),d(d){}
 25     bool operator<( const Pair &p ) const { return d>p.d; }
 26 };
 27 struct Edge {
 28     int u, v, f;
 29     Edge( int u, int v, int f ):u(u),v(v),f(f){}
 30 };
 31 struct Dinic {
 32     int src, dst;
 33     vector<Edge> edge;
 34     vector<int> g[N];
 35     int dep[N], cur[N], qu[N], bg, ed;
 36     void init( int src, int dst ) {
 37         this->src = src;
 38         this->dst = dst;
 39     }
 40     void adde( int u, int v, int f ) {
 41         g[u].push_back( edge.size() );
 42         edge.push_back( Edge(u,v,f) );
 43         g[v].push_back( edge.size() );
 44         edge.push_back( Edge(v,u,0) );
 45     }
 46     bool bfs() {
 47         memset( dep, 0, sizeof(dep) );
 48         qu[bg=ed=1] = src;
 49         dep[src] = 1;
 50         while( bg<=ed ) {
 51             int u=qu[bg++];
 52             for( int t=0; t<g[u].size(); t++ )  {
 53                 Edge &e = edge[g[u][t]];
 54                 if( e.f && !dep[e.v] ) {
 55                     dep[e.v] = dep[e.u]+1;
 56                     qu[++ed] = e.v;
 57                 }
 58             }
 59         }
 60         return dep[dst];
 61     }
 62     dnt dfs( int u, dnt a ) {
 63         if( u==dst || a==0 ) return a;
 64         dnt remain=a, past=0, na;
 65         for( int &t=cur[u]; t<g[u].size(); t++ ) {
 66             Edge &e = edge[g[u][t]];
 67             Edge &ve = edge[g[u][t]^1];
 68             if( e.f && dep[e.v]==dep[e.u]+1 && (na=dfs(e.v,min(remain,e.f))) ) {
 69                 remain -= na;
 70                 past += na;
 71                 e.f -= na;
 72                 ve.f += na;
 73                 if( !remain ) break;
 74             }
 75         }
 76         return past;
 77     }
 78     dnt maxflow() {
 79         dnt rt = 0;
 80         while( bfs() ) {
 81             memset( cur, 0, sizeof(cur) );
 82             rt += dfs(src,0x3f3f3f3f3f3f3f3fll);
 83         }
 84         return rt;
 85     }
 86 }D;
 87  
 88 int n, m;
 89 int head[N], cc[N], dest[M], wght[M], next[M], etot;
 90 dnt dis[2][N];
 91 int in[N], out[N], src, dst, idc;
 92  
 93 void adde( int u, int v, int w ) {
 94     etot++;
 95     dest[etot] = v;
 96     wght[etot] = w;
 97     next[etot] = head[u];
 98     head[u] = etot;
 99 }
100 void dijstra( int src, dnt dis[] ) {
101     static bool done[N];
102     memset( done, false, sizeof(done) );
103     priority_queue<Pair> q;
104     q.push( Pair(src,0) );
105     dis[src] = 0;
106     while( !q.empty() ) {
107         Pair p = q.top();
108         q.pop();
109         int u=p.u;
110         if( done[u] ) continue;
111         done[u] = true;
112         for( int t=head[u]; t; t=next[t] ) {
113             int v=dest[t], w=wght[t];
114             if( dis[v]>dis[u]+w ) {
115                 dis[v]=dis[u]+w;
116                 q.push( Pair(v,dis[v]) );
117             }
118         }
119     }
120 }
121 void makeid() {
122     idc = 0;
123     for( int i=1; i<=n; i++ ) {
124         in[i] = ++idc;
125         out[i] = ++idc;
126     }
127     src = out[1];
128     dst = in[n];
129 }
130 void build() {
131     memset( dis, 0x3f, sizeof(dis) );
132     dijstra( 1, dis[0] );
133     dijstra( n, dis[1] );
134     makeid();
135     D.init( src, dst );
136     for( int i=1; i<=n; i++ ) 
137         if( dis[0][n]==dis[0][i]+dis[1][i] ) 
138             D.adde( in[i], out[i], cc[i] );
139     for( int u=1; u<=n; u++ )
140         for( int t=head[u]; t; t=next[t] ) {
141             int v=dest[t], w=wght[t];
142             if( dis[0][u]+dis[1][v]+w==dis[0][n] )
143                 D.adde( out[u], in[v], oo );
144         }
145 }
146 int main() {
147     scanf( "%d%d", &n, &m );
148     for( int i=1,u,v,w; i<=m; i++ ) {
149         scanf( "%d%d%d", &u, &v, &w );
150         adde( u, v, w );
151         adde( v, u, w );
152     }
153     for( int i=1; i<=n; i++ )
154         scanf( "%d", cc+i );
155     build();
156     printf( "%lld\n", D.maxflow() );
157 }
158 
View Code

 

posted @ 2015-05-24 13:33  idy002  阅读(199)  评论(0编辑  收藏  举报