jzoj3852-单词接龙【0/1分数规划,负环】
生活随笔
收集整理的這篇文章主要介紹了
jzoj3852-单词接龙【0/1分数规划,负环】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目鏈接:https://jzoj.net/senior/#main/show/3852
題目大意
nnn個單詞串,頭尾有兩個相同單詞就可以連在一起,求一個最長的環使得平均單詞長度最長。
解題思路
其實總共26?2626*2626?26個點,然后求一個回路使得平均邊長最長
就是0/1分數規劃問題
二分一個答案midmidmid,將邊權改為mid?wmid-wmid?w,然后跑負環判斷即可。
codecodecode
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=800; const double eps=1e-5; struct node{int to,next,c;double w; }a[100010]; queue<int> q; int n,ls[N],cnt[N],tot; char s[1100]; bool v[N]; double f[N]; void addl(int x,int y,int c){a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].c=c; } bool SPFA(){for(int i=1;i<N;i++)f[i]=0,v[i]=cnt[i]=1,q.push(i);memset(cnt,0,sizeof(cnt));while(!q.empty()){int x=q.front();q.pop();v[x]=0;for(int i=ls[x];i;i=a[i].next){int y=a[i].to;if(f[x]+a[i].w<f[y]){f[y]=f[x]+a[i].w;cnt[y]=cnt[x]+1;if(cnt[y]>=N&&a[i].w<0)return 1;if(!v[y]){v[y]=1;q.push(y);}}}}return 0; } bool check(double x){for(int i=1;i<=tot;i++)a[i].w=x-a[i].c;if(SPFA()) return 1;return 0; } int main() {scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%s",s+1);int l=strlen(s+1);int x=(s[1]-'a')*26+s[2]-'a'+1,y=(s[l-1]-'a')*26+s[l]+1-'a';addl(x,y,l);}double l=2,r=1000;while(r-l>eps){double mid=(l+r)/2;if(check(mid)) l=mid;else r=mid;}if(check(l))printf("%lf",l);else printf("No solution."); }總結
以上是生活随笔為你收集整理的jzoj3852-单词接龙【0/1分数规划,负环】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黑暗的西欧中世纪
- 下一篇: jzoj3854-分组【树状数组,线段树