佛山网站建设方案咨询,网站301定向,网站规划建设方案,软件开发人天报价标准求平均值最小的环#xff0c;如果平均值最小为x#xff0c;则如果把每条边的权值都减(x1)#xff0c;那么新图将会有负环#xff0c;用bellman ford判断。 //#pragma comment(linker, /STACK:1024000000,1024000000)
#includecstdio
#includecstri…求平均值最小的环如果平均值最小为x则如果把每条边的权值都减(x1)那么新图将会有负环用bellman ford判断。 //#pragma comment(linker, /STACK:1024000000,1024000000)
#includecstdio
#includecstring
#includecstdlib
#includealgorithm
#includeiostream
#includesstream
#includecmath
#includeclimits
#includestring
#includemap
#includequeue
#includevector
#includestack
#includeset
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pairint,int pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx1,l,mid
#define rson idx1|1,mid1,r
#define PI 3.1415926535898
templateclass T T min(const T a,const T b,const T c) {return min(min(a,b),min(a,c));
}
templateclass T T max(const T a,const T b,const T c) {return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#elsefreopen(d:\\in1.txt,r,stdin);freopen(d:\\out1.txt,w,stdout);
#endif
}
int getch() {int ch;while((chgetchar())!EOF) {if(ch! ch!\n)return ch;}return EOF;
}struct Edge
{int from,to;double dist;
};
const int maxn55;
vectorint g[maxn];
vectorEdge edge;
double d[maxn];
int inq[maxn];
int inq_cnt[maxn];
int n,m;
void init()
{for(int i1;in;i)g[i].clear();edge.clear();
}
void add(int u,int v,double w)
{Edge e(Edge){u,v,w};edge.push_back(e);g[u].push_back(edge.size()-1);
}
bool negativeCycle(int s)
{queueint q;memset(inq,0,sizeof(inq));memset(inq_cnt,0,sizeof(inq_cnt));for(int i1;in;i){d[i]0;inq[i]1;q.push(i);}while(!q.empty()){int uq.front();q.pop();inq[u]0;for(int i0;ig[u].size();i){Edge eedge[g[u][i]];if(d[u]e.distd[e.to]){d[e.to]d[u]e.dist;if(!inq[e.to]){inq[e.to]1;q.push(e.to);if(inq_cnt[e.to]n)return true;}}}}return false;
}
bool check(double x)
{for(int i0;im;i)edge[i].dist-x;int flagnegativeCycle(1);for(int i0;im;i)edge[i].distx;return flag;
}
int main()
{int t;scanf(%d,t);for(int ca1;cat;ca){scanf(%d%d,n,m);init();for(int i1;im;i){int u,v;double w;scanf(%d%d%lf,u,v,w);add(u,v,w);}double l0,r10000002;//printf(%d\n%d\n,10000000,INF);while(r-l10e-4){double midl(r-l)/2;if(!check(mid))lmid;else rmid;}if(l10000000)printf(Case #%d: No cycle found.\n,ca);elseprintf(Case #%d: %.2lf\n,ca,l);}return 0;
} View Code 转载于:https://www.cnblogs.com/BMan/p/3632934.html