Day1
日哦。。。。苟蒻改死掉发现是数组开太小?!!!!
打了dicnic发现还没有暴力算法来得快?!!!!
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #include<cmath> #include<vector> #include<queue> using namespace std; const int maxn = 1E3 + 10; struct E{ int from,to,cap,flow,cost; }G[20*maxn]; int dis[maxn],fa[maxn],a[maxn],U[5*maxn],V[5*maxn],C[5*maxn],W[5*maxn],L[maxn],cnt[maxn]; int n,m,t,cur,flow,cost,mflow; bool vis[maxn],flag = 1; vector <int> v[maxn]; queue <int> q; bool SPFA() { for (int i = 1; i <= n; i++) dis[i] = ~0U>>1; memset(vis,0,sizeof(vis)); dis[1] = 0; vis[1] = 1; a[1] = ~0U>>1; fa[1] = 0; q.push(1); while (!q.empty()) { int k = q.front(); q.pop(); vis[k] = 0; for (int i = 0; i < v[k].size(); i++) { E e = G[v[k][i]]; if (e.cap > e.flow && dis[e.to] > dis[k] + e.cost) { dis[e.to] = dis[k] + e.cost; fa[e.to] = v[k][i]; a[e.to] = min(a[k],e.cap - e.flow); if (!vis[e.to]) { vis[e.to] = 1; q.push(e.to); } } } } if (dis[n] == ~0U>>1) return 0; int A = a[n]; bool ret = 1; if (!flag && mflow - flow <= A) { A = mflow - flow; ret = 0; } flow += A; cost += dis[n]*A; int u = n; while (u != 1) { G[fa[u]].flow += A; G[fa[u]^1].flow -= A; u = G[fa[u]].from; } return ret; } bool BFS() { memset(L,0,sizeof(L)); memset(cnt,0,sizeof(cnt)); q.push(1); while (!q.empty()) { int k = q.front(); q.pop(); for (int i = 0; i < v[k].size(); i++) { E e = G[v[k][i]]; if (e.cap - e.flow && e.to != 1 && !L[e.to]) { L[e.to] = L[k] + 1; q.push(e.to); } } } return L[n]; } int DFS(int x,int a) { if (x == n || a <= 0) return a; int ret = 0; for (int &i = cnt[x]; i < v[x].size(); i++) { E e = G[v[x][i]]; int f; if (L[e.to] == L[x] + 1 && (f = DFS(e.to,min(a,e.cap - e.flow))) > 0) { a -= f; G[v[x][i]].flow += f; G[v[x][i]^1].flow -= f; ret += f; if (!a) return ret; } } return ret; } void Dicnic() { while (BFS()) flow += DFS(1,~0U>>1); printf("%d ",flow); } int main() { #ifdef YZY freopen("yzy.txt","r",stdin); #endif cin >> n >> m >> t; for (int i = 0; i < m; i++) { scanf("%d%d%d%d",&U[i],&V[i],&C[i],&W[i]); v[U[i]].push_back(cur); G[cur++] = (E){U[i],V[i],C[i],0,0}; v[V[i]].push_back(cur); G[cur++] = (E){V[i],U[i],0,0,0}; } //Dicnic(); while (SPFA()); flag = 0; printf("%d ",flow); mflow = flow + t; for (int i = 0; i < m; i++) { v[U[i]].push_back(cur); G[cur++] = (E){U[i],V[i],~0U>>1,0,W[i]}; v[V[i]].push_back(cur); G[cur++] = (E){V[i],U[i],0,0,-W[i]}; } //for (int i = 0; i < 4*m; i++) G[i].flow = 0; flow = 0; //int x = 0; while (SPFA()); printf("%d",cost); return 0; }