Network flow 24 Software patch problem

Source: Internet
Author: User
Tags strlen

(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; }
Related Article

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.