(Network flow 24 Most need SPJ, so need a SPJ OJ, this series of code is www.oj.swust.edu.cn test pass)
I don't know why there's this strange thing in my network stream, but since it appears, write it. The general idea is to use a binary representation of the state, then each state can be transferred to another State, to find a shortest possible.
TMD i F1 and F2 see the reverse, read the question kill really hopeless.
#include <cstdio> #include <cstdlib> #include <string> #include <cstring> #include <iostream > #include <iomanip> #include <ctime> #include <cmath> #include <algorithm> #include <
Queue> using namespace std;
int b1[10000];
int b2[10000];
int f1[10000];
int f2[10000];
Char s[10000];
int dis[2<<21];
BOOL vis[2<<21];
int v[5000];
Queue<int> que;
int n,m;
void Spfa () {memset (dis,0x3f,sizeof dis);
dis[(1<<n)-1] = 0;
Que.push ((1<<n)-1);
int now;
while (!que.empty ()) {now = Que.front ();
Vis[now] = false;
Que.pop (); for (int i=1,x,y;i<=m;++i) if (! (
Now&b2[i]) && (Now|b1[i]) ==now) {x = now;
x &= (~f1[i]);
x |= f2[i];
if (Dis[x]>dis[now]+v[i]) {dis[x] = Dis[now]+v[i];
if (!vis[x]) { Vis[x] = true;
Que.push (x);
}}}}} int main () {scanf ("%d%d", &n,&m);
for (int i=1;i<=m;i++) {scanf ("%d", &v[i]);
scanf ("%s", s+1);
int Len=strlen (s+1); for (int j=1;j<=len;j++) {if (s[j]== ' + ') b1[i]= (b1[i]| (
1<< (j-1)); else if (s[j]== '-') b2[i]= (b2[i]| (
1<< (j-1));
} scanf ("%s", s+1);
Len=strlen (s+1); for (int j=1;j<=len;j++) {if (s[j]== ' + ') f1[i]= (f1[i]| (
1<< (j-1)); else if (s[j]== '-') f2[i]= (f2[i]| (
1<< (j-1));
} swap (F1[i],f2[i]);
} SPFA ();
if (dis[0]==0x3f3f3f3f) cout<<0;
else cout<<dis[0];
return 0; }