Rokua P2656 Mushroom

Source: Internet
Author: User

Title Description

Chubby and Zyr are going to esqms the forest to pick up mushrooms.

The Esqms Forest has n Grove, M path, each path is one-way, connected to two small bushes, there are a certain number of mushrooms. Chubby and Zyr through a path once, can take the road all the mushrooms. As the Esqms forest is a magical fertile soil, so a mushroom on the road after the harvest, and will grow some new mushrooms, the number of the original number of mushrooms by this road "recovery factor", and then take the whole.

For example, there are 4 mushrooms on one road, the "recovery factor" for this road is 0.7, and the number of mushrooms that can be picked up by this path for the first to fourth time is 4,2,1,0.

Now, Chubby and zyr from Grove S, begging them to pick up as many mushrooms as they can.

For 30% of data, n<=7,m<=15

Another 30% of the data satisfies all "recovery factors" of 0

For 100% of data, the n<=80,000,m<=200,000,0.1<= recovery factor is <=0.8 and only one decimal, 1<=s<=n.

Input/output format

Input format:
First line, N and M

2nd ... M+1 line, 4 numbers per line, respectively, the beginning of a path, the end point, the initial number of mushrooms, recovery factor.

Line M+2, a number s

Output format:
A number that indicates the maximum number of mushrooms to be picked, within the Int32 range.

Input/Output sample

Input Sample #:
3 3
1 2 4 0.5
1 3 7 0.1
2 3 4 0.6
1
Output Example #:
8
Because one side can walk several times so direct run SPFA certainly will T, so we can use Tarjan to shrink point, point right is the total number of mushrooms that can get in the circle, then run SPFA can.

#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <

Stack> #include <queue> using namespace std;
const int sz=401000; struct Edge {int f,t,d;}
ES[SZ],ES2[SZ];
int n,m,sss;
Double K[sz];
int L[sz],r[sz],size[sz];
int Tot,fst[sz],nxt[sz];
    void build (int f,int t,int d) {es[++tot]= (edge) {f,t,d};
    NXT[TOT]=FST[F];
Fst[f]=tot;
    } void build2 (int f,int t,int d) {es2[++tot]= (edge) {f,t,d};
    NXT[TOT]=FST[F];
Fst[f]=tot;
} stack<int>s;
int Dfn[sz],low[sz];
int scc[sz],cnt,cnt2;
int Mg[sz];
    int Trajan (int u) {dfn[u]=low[u]=++cnt;
    S.push (U);
        for (int i=fst[u];i;i=nxt[i]) {int v=es[i].t;//cout<<v<< "" <<dfn[v]<<endl;
            if (!dfn[v]) {Low[v]=trajan (v);
        Low[u]=min (Low[u],low[v]);
    } if (!scc[v]) low[u]=min (Low[u],dfn[v]); }//cout<<u<< "" <<dfn[u]<< "" <<low[u]<<eNdl
        if (Dfn[u]==low[u]) {cnt2++;
            while (1) {int ha=s.top (); S.pop ();
        Scc[ha]=cnt2;if (ha==u) break;
}} return Low[u];
} queue<int>q;
BOOL Vis[sz];
int DIS[SZ],MAXN;
    void SPFA (int ss) {vis[ss]=1;
    Q.push (ss);d IS[SS]=MG[SS];
        while (!q.empty ()) {int U=q.front ();
        Q.pop ();
        vis[u]=0;
            for (int i=fst[u];i;i=nxt[i]) {int v=es2[i].t;
                if (DIS[V]&LT;DIS[U]+MG[V]+ES2[I].D) {dis[v]=dis[u]+mg[v]+es2[i].d;
                    if (!vis[v]) {vis[v]=1;
                Q.push (v);
    }}}}} int main () {scanf ("%d%d", &n,&m);
        for (int i=1;i<=m;i++) {scanf ("%d%d%d%lf", &l[i],&r[i],&size[i],&k[i]);
    Build (L[i],r[i],size[i]);
    } scanf ("%d", &sss);
    Trajan (SSS); for (int i=1;i<=m;i++)//Indent {if (Scc[l[i]]==scc[r[i]]) {while (Size[i]) {mg[scc[l[i
                ]]]+=size[i];
                cout<<mg[scc[l[i]]]<<endl;
            Size[i]*=k[i];
    }}} memset (Fst,0,sizeof (FST));
    memset (nxt,0,sizeof (NXT));
    tot=0;
    for (int i=1;i<=m;i++) {if (Scc[l[i]]!=scc[r[i]]) build2 (Scc[l[i]],scc[r[i]],size[i]);
    } SPFA (Scc[sss]);
    for (int i=1;i<=cnt2;i++) Maxn=max (Maxn,dis[i]);
    cout<<maxn;
return 0; }
Related Keywords:
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.