圖論&數學:最小平均值環

POJ2989:求解最小平均值環優化

最優化平均值的顯然作法是01分數規劃spa

給定一個帶權有向圖code

對於這個圖中的每個環blog

定義這個環的價值爲權值之和的平均值get

對於全部的環,求出最小的平均值string

這個結論怎麼作的我找不到,可是顯然的作法是能夠找到的it

因爲Average=(E1+E2+…..+Ek)/K 因此Average*K=E1+E2+……+Ek 即(E1-Average)+(E2-Average)+….+ (Ek-Average)=0 另外注意到上式中的等於號能夠改寫爲小於等於,那麼咱們能夠二分答案Ans,而後判斷是否存在一組解知足(E1+E2+…..+Ek)/K>Ans,即判斷 (E1- Ans)+(E2- Ans)+….+ (Ek- Ans)>0 因而在二分答案後,咱們把邊的權值更新,問題就變成了查找圖中是否存在一個正環

也就是二分答案+spfa判斷正環io

而後學到了,DFS的SPFA判環賊快class

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;  5 #define ll long long
 6 #define inf 0x3f3f3f3f
 7 #define N 700
 8 #define M 100010
 9 #define eps 1e-4
10 inline int read(){ 11     int x=0,f=1;char ch=getchar(); 12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 13     while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); 14     return x*f; 15 } 16 int n,h[N],num; 17 double d[N]; 18 char s[1010]; 19 bool vis[N]; 20 struct edge{ 21     int to,next,val; 22 }data[M]; 23 inline void add(int x,int y,int val){ 24     data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].val=val; 25 } 26 inline int calc(char a,char b){return (a-'a')*26+b-'a'+1;} 27 bool dfs(int x,double mid){//判正環 
28     vis[x]=1; 29     for(int i=h[x];i;i=data[i].next){ 30         int y=data[i].to; 31         if(d[x]+data[i].val-mid>d[y]){ 32             d[y]=d[x]+data[i].val-mid; 33             if(vis[y]||dfs(y,mid)){vis[x]=0;return 1;} 34  } 35     }vis[x]=0;return 0; 36 } 37 inline bool jud(double mid){ 38     memset(d,0,sizeof(d)); 39     for(int i=1;i<=26*26;++i) if(dfs(i,mid)) return 1; 40     return 0; 41 } 42 int main(){ 43 // freopen("a.in","r",stdin);
44     while(1){ 45         n=read();if(!n) break;num=0;memset(h,0,sizeof(h)); 46         while(n--){ 47             scanf("%s",s+1);int len=strlen(s+1); 48             add(calc(s[1],s[2]),calc(s[len-1],s[len]),len); 49  } 50         double l=0,r=1000; 51         while(r-l>=eps){ 52             double mid=(r+l)/2; 53             if(jud(mid)) l=mid; 54             else r=mid; 55  } 56         if(l==0) puts("No solution."); 57         else printf("%.2f\n",l); 58  } 59     return 0; 60 }
相關文章
相關標籤/搜索