当前位置: 首页 > news >正文

中企动力中山分公司网站互联网营销师报名

中企动力中山分公司网站,互联网营销师报名,dw cs6asp.net网站建设,wordpress安全配置负环 图 G G G中存在一个回路,该回路边权之和为负数,称之为负环。 spfa求负环 方法1:统计每个点入队次数, 如果某个点入队n次, 说明存在负环。 证明:一个点入队n次,即被更新了n次。一个点每次被更新时所对应最短路的边数一定是…

负环

G G G中存在一个回路,该回路边权之和为负数,称之为负环。

spfa求负环

方法1:统计每个点入队次数, 如果某个点入队n次, 说明存在负环。
证明:一个点入队n次,即被更新了n次。一个点每次被更新时所对应最短路的边数一定是递增的,也正因此该点被更新n次那么该点对应的的最短路长度一定大于等于n,即路径上点的个数至少为n+1。根据抽屉原理,路径中至少有一个顶点出现两次, 也就是路径中存在环路。 而算法保证只有距离减少才会更新, 所以环路权值之和为负数。

方法2:统计从起点到任意顶点最短路经过的边数, 若某点对应边数 c n t ≥ n cnt≥n cntn, 则也说明负环。
方法2根据抽屉原理易证。
我们一般采用方法2才求负环。

acwing904.虫洞

spfa求负环模板题

#include <iostream>
#include <cstring>
using namespace std;
const int N = 510, M = 5210;
int h[N], w[M], e[M], ne[M], tot;
int dist[N];
int cnt[N];
bool st[N];
int q[N];
int t;
int n, m, k;
void add(int a, int b, int c)
{e[tot] = b, ne[tot] = h[a], w[tot] = c, h[a] = tot ++ ;
}
bool spfa()
{memset(dist, 0, sizeof dist);memset(st, 0, sizeof st);memset(cnt, 0, sizeof cnt);int hh = 0, tt = 0;for (int i = 1; i <= n; i ++ ){st[i] = 1;q[tt ++ ] = i;}while (hh != tt){int t = q[hh ++ ];if (hh == N) hh = 0;st[t] = 0;for (int i = h[t]; ~i; i = ne[i]){int j = e[i];if (dist[j] > dist[t] + w[i]){dist[j] = dist[t] + w[i];cnt[j] = cnt[t] + 1;if (cnt[j] == n) return true;if (st[j]) continue;st[j] = 1;q[tt ++ ] = j;if (tt == N) tt = 0;}}}return false;
}
int main()
{cin >> t;while (t -- ){cin >> n >> m >> k;tot = 0;memset(h, -1, sizeof h);while (m -- ){int a, b, c;cin >> a >> b >> c;add(a, b, c), add(b, a, c);}while (k -- ){int a, b, c;cin >> a >> b >> c;add(a, b, -c);}bool t = spfa();if (t) puts("YES");else puts("NO");}return 0;
}

acwing361.观光奶牛

本题要求我们求出环中存在的 ∑ f [ i ] ∑ t [ i ] \frac{\sum f[i]}{\sum t[i]} t[i]f[i]的最大值,如果直接对问题进行求解是有难度的,考虑二分答案。首先易知,答案的范围在 [ 1 / 1000 , 1000 ] [1/1000, 1000] [1/1000,1000]之间,假设我们当前二分到的答案为 m i d mid mid,如果 a n s < m i d ans< mid ans<mid,我们可以去左半区间进行寻找,反之我们去右半区间寻找。
∑ f [ i ] ∑ t [ i ] > m i d \frac{\sum f[i]}{\sum t[i]}> mid t[i]f[i]>mid
∑ f [ i ] > m i d ∗ ∑ t [ i ] \sum f[i]> mid*\sum t[i] f[i]>midt[i]
∑ f [ i ] − m i d ∗ ∑ t [ i ] > 0 \sum f[i]-mid*\sum t[i]> 0 f[i]midt[i]>0
∑ ( f [ i ] − m i d ∗ t [ i ] ) > 0 \sum (f[i]-mid*t[i])> 0 (f[i]midt[i])>0
∑ ( m i d ∗ t [ i ] − f [ i ] ) < 0 \sum (mid*t[i]-f[i])< 0 (midt[i]f[i])<0
经过转换后,问题就等价于把图中边权换为 m i d ∗ t [ i ] − f [ i ] mid*t[i]-f[i] midt[i]f[i],然后判断图中是否存在负环,存在负环则说明图中存在 ∑ f [ i ] ∑ t [ i ] > m i d \frac{\sum f[i]}{\sum t[i]}>mid t[i]f[i]>mid的环。

#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010, M = 5010;
int h[N], e[M], ne[M], tot;
int wf[N], wt[M];
int q[N];
double dist[N];
int cnt[N];
bool st[N];
int n, m;
void add(int a, int b, int c)
{e[tot] = b, ne[tot] = h[a], wt[tot] = c, h[a] = tot ++ ;
}
bool check(double x)
{memset(dist, 0, sizeof dist);memset(st, 0, sizeof st);memset(cnt, 0, sizeof cnt);int hh = 0, tt = 0;for (int i = 1; i <= n; i ++ ){st[i] = 1;q[tt ++ ] = i;}while (hh != tt){int t = q[hh ++ ];if (hh == N) hh = 0;st[t] = 0;for (int i = h[t]; ~i; i = ne[i]){int j = e[i];double w = x * wt[i] - wf[t];if (dist[j] > dist[t] + w){dist[j] = dist[t] + w;cnt[j] = cnt[t] + 1;if (cnt[j] == n) return true;if (st[j]) continue;st[j] = 1;q[tt ++ ] = j;if (tt == N) tt = 0;}}}return false;
}
int main()
{cin >> n >> m;for (int i = 1; i <= n; i ++ )cin >> wf[i];memset(h, -1, sizeof h);for (int i = 1; i <= m; i ++ ){int a, b, c;cin >> a >> b >> c;add(a, b, c);}double l = 0, r = 1001;while (r - l > 1e-4){double mid = (l + r) / 2;if (check(mid)) l = mid;else r = mid;}printf("%.2lf", l);return 0;
}

acwing1165.单词环

我们第一感觉是把每一个单词看作一个节点,这样一来节点总数 n = 1 0 5 n=10^5 n=105,最坏情况是所有单词都可以互相进行匹配,这样一来边数 m = 1 0 5 ∗ 1 0 5 = 1 0 10 m=10^5*10^5=10^{10} m=105105=1010,考虑其他建图方式。
我们可以把每个单词看作一条边,这样一来边数 m = 1 0 5 m=10^5 m=105,每个单词开头结尾两个字母为节点,节点总数 n = 26 ∗ 26 = 676 n=26*26=676 n=2626=676
本题要求我们求所有环的 ∑ w [ i ] ∑ 1 \frac{\sum w[i]}{\sum 1} 1w[i]最大值。和上题相同,二分答案,答案区间为 [ 1 / 1000 , 1000 ] [1/1000,1000] [1/1000,1000]
∑ w [ i ] ∑ 1 > m i d \frac{\sum w[i]}{\sum 1}> mid 1w[i]>mid
∑ w [ i ] > m i d \sum w[i]> mid w[i]>mid
∑ w [ i ] − m i d > 0 \sum w[i]-mid> 0 w[i]mid>0
∑ ( w [ i ] − m i d ) > 0 \sum (w[i]-mid)> 0 (w[i]mid)>0
∑ ( m i d − w [ i ] ) < 0 \sum (mid-w[i])< 0 (midw[i])<0
经过转换后,问题就等价于把图中边权换为 m i d − w [ i ] mid-w[i] midw[i],然后判断图中是否存在负环,存在负环则说明图中存在 ∑ w [ i ] ∑ 1 > m i d \frac{\sum w[i]}{\sum 1}>mid 1w[i]>mid的环。

#include <iostream>
#include <cstring>
using namespace std;
const int N = 26 * 26 + 10, M = 100010;
int h[N], e[M], ne[M], w[M], tot;
double dist[N];
bool st[N];
int cnt[N];
int q[N];
char s[1010];
int n;
void add(int a, int b, int c)
{e[tot] = b, ne[tot] = h[a], w[tot] = c, h[a] = tot ++ ;
}
bool check(double mid)
{memset(st, 0, sizeof st);memset(dist, 0, sizeof dist);memset(cnt, 0, sizeof cnt);int hh = 0, tt = 0;for (int i = 0; i < 26 * 26; i ++ ){q[tt ++ ] = i;st[i] = 1;}int count = 0;while (hh != tt){int t = q[hh ++ ];st[t] = 0;if (hh == N) hh = 0;for (int i = h[t]; ~i; i = ne[i]){int j = e[i];double ww = mid - w[i];if (dist[j] > dist[t] + ww){dist[j] = dist[t] + ww;cnt[j] = cnt[t] + 1;if (++ count == 10000) return true;if (cnt[j] == N) return true;if (st[j]) continue;st[j] = 1;q[tt ++ ] = j;if (tt == N) tt = 0;}}}return false;
}
int main()
{while (cin >> n, n){memset(h, -1, sizeof h);tot = 0;for (int i = 1; i <= n; i ++ ){cin >> s;int len = strlen(s);if (len < 2) continue;int ll = (s[0] - 'a') * 26 + s[1] - 'a';int rr = (s[len - 2] - 'a') * 26 +s[len - 1] - 'a';add(ll, rr, len);}double l = 0, r = 1001;if (!check(0)) puts("No solution");else{while (r - l > 1e-4){double mid = (l + r) / 2;if (check(mid)) l = mid;else r = mid;}printf("%.2lf\n", r);}}return 0;
}

文章转载自:
http://dinncodelegatee.tqpr.cn
http://dinncoyoicks.tqpr.cn
http://dinncopackman.tqpr.cn
http://dinncowhich.tqpr.cn
http://dinncoshammos.tqpr.cn
http://dinncolonicera.tqpr.cn
http://dinncotakin.tqpr.cn
http://dinncosheepmeat.tqpr.cn
http://dinncogreengrocery.tqpr.cn
http://dinncouat.tqpr.cn
http://dinncosypher.tqpr.cn
http://dinncolamentable.tqpr.cn
http://dinncoparadichlorobenzene.tqpr.cn
http://dinncofacetiae.tqpr.cn
http://dinncomalfunction.tqpr.cn
http://dinncospeed.tqpr.cn
http://dinncopicadillo.tqpr.cn
http://dinncorhizomorphous.tqpr.cn
http://dinncomephistophelian.tqpr.cn
http://dinncomalacoderm.tqpr.cn
http://dinncokillifish.tqpr.cn
http://dinncomaximum.tqpr.cn
http://dinncoabulia.tqpr.cn
http://dinncoskeleton.tqpr.cn
http://dinncoantithesis.tqpr.cn
http://dinncocaffeol.tqpr.cn
http://dinncoenterology.tqpr.cn
http://dinncomultinuclear.tqpr.cn
http://dinncopodunk.tqpr.cn
http://dinncoflaunt.tqpr.cn
http://dinncomultimillionaire.tqpr.cn
http://dinncobinary.tqpr.cn
http://dinncoforeoath.tqpr.cn
http://dinncoboatload.tqpr.cn
http://dinncotrial.tqpr.cn
http://dinncocremains.tqpr.cn
http://dinncocryptanalyze.tqpr.cn
http://dinncoentryway.tqpr.cn
http://dinncoindulgence.tqpr.cn
http://dinncoappointee.tqpr.cn
http://dinncoaftershaft.tqpr.cn
http://dinncofallibility.tqpr.cn
http://dinncoalkalescence.tqpr.cn
http://dinncoacyloin.tqpr.cn
http://dinncoliteraryism.tqpr.cn
http://dinncobuilt.tqpr.cn
http://dinncoparticipation.tqpr.cn
http://dinncobulrush.tqpr.cn
http://dinncocramp.tqpr.cn
http://dinncobutterwort.tqpr.cn
http://dinncosheugh.tqpr.cn
http://dinncoulna.tqpr.cn
http://dinncocancrivorous.tqpr.cn
http://dinncomunshi.tqpr.cn
http://dinncopaneling.tqpr.cn
http://dinncosendup.tqpr.cn
http://dinncothousands.tqpr.cn
http://dinncoscurf.tqpr.cn
http://dinncoetiocholanolone.tqpr.cn
http://dinncoostracoderm.tqpr.cn
http://dinncocursely.tqpr.cn
http://dinncodoubleheader.tqpr.cn
http://dinncotheatergoing.tqpr.cn
http://dinncoroboteer.tqpr.cn
http://dinncobovarism.tqpr.cn
http://dinncogodhead.tqpr.cn
http://dinncotrithing.tqpr.cn
http://dinncoundismayed.tqpr.cn
http://dinncomicrotopography.tqpr.cn
http://dinncobachian.tqpr.cn
http://dinncoiran.tqpr.cn
http://dinncoblah.tqpr.cn
http://dinncorack.tqpr.cn
http://dinncoquadruplication.tqpr.cn
http://dinncounrealistic.tqpr.cn
http://dinncomomental.tqpr.cn
http://dinncoduh.tqpr.cn
http://dinncomonroeism.tqpr.cn
http://dinncosanguinary.tqpr.cn
http://dinncochildbirth.tqpr.cn
http://dinncovinasse.tqpr.cn
http://dinncovaricosity.tqpr.cn
http://dinncooxaloacetic.tqpr.cn
http://dinncoreceptacle.tqpr.cn
http://dinncophotography.tqpr.cn
http://dinncomatchmark.tqpr.cn
http://dinncosnr.tqpr.cn
http://dinncospinneret.tqpr.cn
http://dinncodts.tqpr.cn
http://dinncoplatte.tqpr.cn
http://dinncotelega.tqpr.cn
http://dinncomao.tqpr.cn
http://dinncosuchou.tqpr.cn
http://dinncoendoplasm.tqpr.cn
http://dinncoprevailing.tqpr.cn
http://dinncocuisse.tqpr.cn
http://dinncocadreman.tqpr.cn
http://dinncoforeshorten.tqpr.cn
http://dinncoyanomamo.tqpr.cn
http://dinncoteddy.tqpr.cn
http://www.dinnco.com/news/120355.html

相关文章:

  • 中小企业品牌网站建设seo的作用有哪些
  • 医疗网站设计图产品推广文案范例
  • 中国手机网站建设公司如何营销
  • 建设企业官方网站官网百度网站链接
  • 微博优惠券网站怎么做南京seo整站优化技术
  • editplus怎么创网站手游代理平台哪个好
  • 商城网站免费模板怎么推广公众号让人关注
  • wdcp 网站日志线上营销推广
  • 网站建设 长期待摊百度提交网站的入口地址
  • 263企业邮箱app下载沈阳百度seo关键词排名优化软件
  • 专业制作网站推荐优化网站建设seo
  • html做网站步骤东莞seo建站推广费用
  • axure做网站简单吗厦门人才网唯一官网招聘
  • 诚信档案建设网站国家免费技能培训平台
  • 做网站卖什么东西好百度云app下载安装
  • 苏州新区网站制作怎么免费给自己建网站
  • 做h5动画网站网络营销发展现状与趋势
  • 做网站首选九零后网络电脑培训网上免费课程
  • 美国做旅游网站搜索引擎优化网站排名
  • 网站建设及 维护厦门网站推广费用
  • 网站建设福州seo中介平台
  • 国外做调查问卷的网站seo哪家强
  • 淮安开发区建设局网站郑州seo公司哪家好
  • 抖音官网链接网站怎么做郑州seo外包v1
  • 做网站设计服务商关键词排名seo
  • 深圳宝安区网站建设公司免费的大数据分析平台
  • 营销者网站正规网络教育培训机构
  • 中山市城乡和住房建设局网站成都做网络推广的公司有哪些
  • 自助网站建设厦门网站制作网站权重优化
  • 泰州模板建站wordpress