Summary of three types of 01 fractional programming

Source: Internet
Author: User

First, the generation of tree-type

Given a weighted a[i],b[i graph, each side has two weights, and a spanning tree for the graph, making Sigam (A[i])/sigam (b[i) the smallest.


Parsing: We assume that we have found a solution K, a new solution is better than K

Then Sigam (A[i])/sigam (B[i]) <=k

i.e. Sigam (A[i])-k*sigam (b[i)) <=0

Sigam (A[i]-k*b[i]) <=0

Then we just need to ask the minimum value of ans of the Sigam (A[i]-b[i]) on the left of the inequality to compare the relationship between ANS and 0 to determine whether the current solution is optimal.


So the process of our algorithm is this:

(1) two points a K

(2) Building the map with the new Benquan A[i]-k*b[i]

(3) To find the minimum spanning tree

(4) To determine whether the current solution is optimal


Example: "jzyzoj1636" the old driver racing

Now there are N groups need to Zhang Yijie the old driver's resources, in order to facilitate the expression, we use 1 to n these n numbers to represent these n groups. Zhang Yijie is now in Group 1, and because of the reason for the network alarm, the transmission of resources between each group has a specific time and risk of taking, and resources can only be transferred between two groups at the same time. To highlight the programmer's push, each group is represented in a three-dimensional space as a coordinate, and the time required to transfer resources between two groups is defined as the line distance projected by these two groups in the Xoy plane, while the hazard is defined as the difference of the third dimensional coordinates of the two groups. Zhang Yijie want the minimum average risk per unit time, that is, the ratio of total transmission time to the total risk of commitment, please help the old driver find this ratio

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime
> #include <cmath> #include <algorithm> #include <iomanip> using namespace std; #define EPS 0.0005 #define FILE "read" #define MAXN 1010 #define UP (i,j,n) for (int i=j;i<=n;i++) #define DN (i,j,n) for (
	int i=j;i>=n;i--) namespace init{char buf[1<<15],*fs,*ft;
	inline char getc () {return (fs==ft&& ft= (fs=buf) +fread (Buf,1,1<<15,stdin), fs==ft))? 0:*fs++;  inline int read () {int x=0,f=1;
		Char ch=getc ();  while (!isdigit (CH)) {if (ch== '-') f=-1;
		Ch=getc ();}  while (IsDigit (ch)) {x=x*10+ch-' 0 ';
		Ch=getc ();}
	return x*f;
}}using namespace INIT;
	struct node{int x,y,h;double v; BOOL operator< (const node PP) const {return v<pp.v;}}
E[MAXN*MAXN],E[MAXN*MAXN];
int N,LEN,LEN,X[MAXN],Y[MAXN],V[MAXN],F[MAXN]; 
BOOL CMP (node A,node b) {return A.V&LT;B.V.} int find (int x) {return f[x]==x?x:f[x]=find (f[x));} void Insert (int i,int j) {	E[++len].x=i;  
	E[len].y=j;
	E[LEN].V=SQRT (Double) ((X[i]-x[j]) * (X[i]-x[j]) + (Y[I]-Y[J)) * (Y[I]-Y[J)));
E[len].h=abs (V[i]-v[j]);  } void Insert (int x,int y,double v) {e[++len].x=x;  E[len].y=y;
E[len].v=v;}
	Double check (double k) {len=0;
	Up (I,1,len) Insert (E[I].X,E[I].Y,E[I].H-K*E[I].V);
	Up (i,1,n) f[i]=i;
	Sort (e+1,e+len+1); Double ans=0;
	int sum=0;
		Up (i,1,len) {int P=find (e[i].x), Q=find (E[I].Y);
		if (p!=q) f[p]=q,ans+=e[i].v,sum++;
	if (sum==n-1) break;
return ans;
	int main () {n=read ();  Up (i,1,n) {x[i]=read ();  Y[i]=read ();
		V[i]=read ();
	Up (j,1,i-1) insert (I,J);
	Double left=0.0001,right=10;
		while (left+0.0003<=right) {double mid= (left+right)/2.0;
		if (check (mid) <=0) Right=mid;
	else Left=mid;
	Double Ans=check (left) left:right;
	printf ("%.3lf\n", ans);
return 0; }


Second, the smallest cut type

Given a weighted graph, to make the path from S to T to be connected, we need to delete the C edge and the Sigam (vi)/C for the minimum value of the C bar.


Parsing: As with the above question, we still assume that we have found a solution K, then a better solution needs to meet the condition is Sigam (vi)/c<=k

namely Sigam (vi)-c*k<=0

Sigam (vi-k) <=0

So we just need to ask for the minimum value on the left side of the inequality, and we can judge the relationship between it and 0.


Then the process of the algorithm is as follows:

(1) Two points a K value

(2) using the new Benquan vi-k to build the map

(3) Run the smallest cut

(4) To determine whether the current solution is optimal

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime
> #include <cmath> #include <algorithm> #include <iomanip> using namespace std; #define FILE "read" #define INF 1000000000 #define EPS 0.00001 #define MAXM 4050 #define MAXN 1050 #define UP (i,j,n) for (i
NT i=j;i<=n;i++) #define DN (i,j,n) for (int i=j;i>=n;i--) const double OO=9999999999999LL;
	namespace init{Char buf[1<<15],*fs,*ft;
	inline char getc () {return (fs==ft&& ft= (fs=buf) +fread (Buf,1,1<<15,stdin), fs==ft))? 0:*fs++;  inline int read () {int x=0,f=1;
		Char Ch=getchar ();  while (!isdigit (CH)) {if (ch== '-') f=-1;
		Ch=getchar ();}  while (IsDigit (ch)) {x=x*10+ch-' 0 ';
		Ch=getchar ();}
	return x*f;
}}using namespace INIT; struct Node{int x,y,next,rel;double v}
E[MAXM*4]; int n,m,len,top,link[maxn],q[maxn],level[maxn],x[maxn],y[maxn],v[maxn],stack[maxn],col[maxn],vis[maxn];\ BOOL Comp (double a,double b) {return abs (A-b) <eps;}
void Insert (int x,int y,double v) {e[++len].next=link[x];
	link[x]=len;e[len].y=y;e[len].v=v;e[len].rel=len+1; E[++len].next=link[y];
link[y]=len;e[len].y=x;e[len].v=0;e[len].rel=len-1;
	BOOL BFs () {memset (level,-1,sizeof (level));  int head=0,tail=1;  level[1]=0;
	Q[1]=1;
				while (++head<=tail) {for (int i=link[q[head]];i;i=e[i].next) {if (!comp (e[i].v,0) &&level[e[i].y]<0) {
				Q[++TAIL]=E[I].Y;
			level[q[tail]]=level[q[head]]+1;
}} return level[n]>=0;
	Double maxflow (int x,double flow) {if (x==n) return flow;
	Double maxflow=0,d=0; for (int i=link[x];i&&maxflow<flow;i=e[i].next) {if Level[x]+1==level[e[i].y]&&!comp (e[i].v,0)  {if (D=maxflow (E[i].y,min (E[i].v,flow-maxflow))) {E[i].v-=d;  E[e[i].rel].v+=d;
			Maxflow+=d;
	}} if (!maxflow) level[x]=-1;
return maxflow;
	Double Solve () {double d,sum=0;
	while (BFS ()) while (D=maxflow (1,oo*1.0)) Sum+=d;
return sum;
	BOOL Check (double k) {double sum=0;len=0;top=0; MemsET (link,0,sizeof (Link));
	memset (Vis) (vis,0,sizeof);
		Up (i,1,m) {double temp=v[i]-k;
		if (temp<=0) sum+=temp,stack[++top]=i,vis[i]=1;
	else Insert (x[i],y[i],temp), insert (Y[I],X[I],TEMP);
	} sum+=solve ();
Return sum<=0;
	void Dfs (int x) {if (col[x]) return;
	Col[x]=1;
for (int i=link[x];i;i=e[i].next) if (!comp (e[i].v,0)) DFS (E[I].Y);  int main () {n=read ();
	M=read ();
	Up (i,1,m) X[i]=read (), Y[i]=read (), V[i]=read ();
	Double left=1,right=1000000000;
		while (left+eps<right) {double mid= (left+right)/2.0;
		if (check (mid)) Right=mid;
	else Left=mid;
	} if (!check) check (right);
	DFS (1);
	Up (I,1,m) if (col[x[i]]!=col[y[i]]&&!vis[i]) stack[++top]=i;
	printf ("%d\n", top);
	Sort (stack+1,stack+top+1);
	Up (i,1,top) printf ("%d", stack[i]);
	printf ("\n%d\n", Clock ());
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.