标签:分析 ref read 产生 problem -- 强连通分量 iostream http
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
const int N=100005;
const int M=200005;
int n,m,S,ans,dis[N],a[M],b[M],c[M],size[N],in[N];
int tim,top,num,dfn[N],low[N],st[N],color[N];
int tot,head[N],nxt[M],to[M],w[M];
double d[M],ww[M];queue<int>q;
inline void add(int a,int b,int c,double d){
nxt[++tot]=head[a];head[a]=tot;
to[tot]=b;w[tot]=c;ww[tot]=d;
}
inline void Add(int a,int b,int c){
nxt[++tot]=head[a];head[a]=tot;
to[tot]=b;w[tot]=c;
}
inline void tarjan(int u){
dfn[u]=low[u]=++tim;st[++top]=u;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!color[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
color[u]=++num;
while(st[top]!=u){
color[st[top]]=num;
--top;
}
--top;
}
}
inline int calc(int s,int hf){
int sum=0;
while(s){
sum+=s;
s=(int)s*hf;
}
return sum;
}
int main(){
n=read();m=read();
for(int i=1;i<=m;++i){
a[i]=read();b[i]=read();c[i]=read();cin>>d[i];
add(a[i],b[i],c[i],d[i]);
}
S=read();
for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i);
tot=0;memset(head,0,sizeof(head));
for(int i=1;i<=m;++i){
if(color[a[i]]!=color[b[i]])
Add(color[a[i]],color[b[i]],w[i]);
else{//如果这条边在强连通分量里面就计算贡献
int s=w[i];
while(s){
size[color[a[i]]]+=s;
s=(int)s*ww[i];
}
}
}
memset(dis,-1,sizeof(dis));
q.push(color[S]);dis[color[S]]=size[color[S]];//初始化
while(q.size()){
int u=q.front();q.pop();in[u]=0;
for(int i=head[u];i;i=nxt[i]){
int v=to[i];
if(dis[v]<dis[u]+w[i]+size[v]){//最长路
dis[v]=dis[u]+w[i]+size[v];
if(!in[v])in[v]=1,q.push(v);
}
}
}
for(int i=1;i<=num;++i)ans=max(ans,dis[i]);//找到最大值
printf("%d\n",ans);
return 0;
}
标签:分析 ref read 产生 problem -- 强连通分量 iostream http
原文地址:https://www.cnblogs.com/PPXppx/p/11579314.html