int mincost(int a[][5], int side, int i, int j)
	{
		pair<int, int> *loc;
		if (p[i][j] != INT_MIN)
		{
			/*loc = new pair<int, int>(i, j);
			path.push_back(loc);*/
			return p[i][j];
		}
		if (i == side - 1 && j == side - 1)
		{
			/*loc = new pair<int, int>(i, j);
			path.push_back(loc);*/
			p[i][j] = a[i][j];
			return p[i][j];
		}

		int right = INT_MAX;
		if (i<side-1)
		right = mincost(a, side, i + 1, j);
		int down = INT_MAX;
		if (j<side-1)
		down = mincost(a, side, i, j + 1);
		int diag = INT_MAX;
		if (i<side-1 && j<side-1)
		diag = mincost(a, side, i + 1, j + 1);

		if (right <= down && right <= diag)
		{
			loc = new pair<int, int>(i+1, j);
			p[i][j] = right + a[i][j];
		}
		if (down <= right && down <= diag)
		{
			loc = new pair<int, int>(i, j+1);
			p[i][j] = down + a[i][j];
		}
		if (diag <= right && diag <= down)
		{
			loc = new pair<int, int>(i + 1, j+1);
			p[i][j] = diag + a[i][j];
		}

		//path.push_back(loc);
		return p[i][j];
	}
Esempio n. 2
0
int main(){

	int i, N, K;
	int C[102];
	
	scanf("%d %d", &N, &K);
	for(i=0; i<N; i++){
		scanf("%d", &(C[i]));
	}
	
	int result = mincost(C, N, K);
	printf("%d\n", result);
	
	return 0;
}
Esempio n. 3
0
  void mincost(int city)
  {
  int i,ncity;
  visited[city]=1;
  printf("%d->",city);
  ncity=least(city);
  if(ncity==999)
  {
  ncity=1;
  printf("%d",ncity);
  cost+=a[city][ncity];
  return;
  }
  mincost(ncity);

  }
Esempio n. 4
0
  void main()
  {
  float ratio;
  clrscr();
  get();
  printf("\n\nthe path is:\n\n");
  mincost(1);
  put();
  printf("\nthe pathis (using approximation algo):\n");
  nearest_neighbour(1);
  printf("1");
  sum=sum+a[lastcity][1];

  printf("minimum cost is%d\n",sum);
  ratio=(float)sum/cost;
  printf("the accuracy ratio is %f\n",ratio);
  getch();
  }
Esempio n. 5
0
int main()
{
    int cas=0;
    while (scanf("%d%d",&n,&m)==2&&n)
    {
	memset(head,-1,sizeof(head));
	cnt=0; S=n; T=n+1;
	addedge(S,0,2,0); addedge(n-1,T,2,0);
	while (m--)
	{
	    int u,v,w;
	    scanf("%d%d%d",&u,&v,&w);
	    addedge(u,v,1,w);
	}
	int ans=mincost();
	if (ans==-1) printf("Instance #%d: Not possible\n",++cas);
	else printf("Instance #%d: %d\n",++cas,ans);
    }
    return 0;
}
Esempio n. 6
0
File: f.c Progetto: noodles-v6/ACM
void solve() {
	char s[MAXSTR],t[MAXSTR];
	ll mask1=0,mask2=0,mask3;
	int i,j,tn;
	int source,sink,letters,words;
	int match[MAXSTR];
	fgets(s,MAXSTR-1,stdin); trimcrlf(s);
	for(i=j=0;;) {
		while(s[j]==' ') j++;
		if(!s[j]) break;
		t[i++]=s[j++];
	}
	t[i]=0;
	mask1=(1LL<<getix(t[0]))|(1LL<<getix(t[1]));
	for(i=2;t[i];i++) {
		if(!isalpha(t[i])) printf("illegal char [%c]\n",t[i]),exit(0);
		mask2|=(1LL<<getix(t[i]));
	}
	tn=strlen(t);
	for(ni=i=0;i<nw;i++) if((wmask[i]&mask1)==mask1 && (wmask[i]&mask2)) include[ni++]=i;
	if(ni<tn-2) {
		/* trivial reject - fewer eligible words than letters */
		puts("-1");
		return;
	}
	/* create graph */
	source=0;
	letters=1;
	words=letters+tn-2;
	sink=words+ni;
	n=sink+1;
	ne=0;
	if(n>MAXV) puts("increase MAXV and recompile"),exit(0);
	for(i=0;i<tn-2;i++) addedge(source,letters+i);
	for(i=0;i<tn-2;i++) for(j=0;j<ni;j++) {
		mask3=mask1|(1LL<<getix(t[i+2]));
		if((wmask[include[j]]&mask3)==mask3) addedge(letters+i,words+j);
	}
	for(i=0;i<ni;i++) addedge(words+i,sink);
	radixsort();
	inverseedges();
	memset(f,0,ne*sizeof(int));
	memset(cost,0,ne*sizeof(int));
	/* set capacities and costs */
	for(i=0;i<ne;i++) {
		if(from[i]==source) f[i]=1;
		if(to[i]==sink) f[i]=1,cost[i]=strlen(w[include[from[i]-words]]);
		if(from[i]==sink) f[i]=1,cost[i]=-strlen(w[include[to[i]-words]]);
		if(from[i]>=letters && from[i]<words && to[i]>=words && to[i]<sink) f[i]=1;
	}
	mincost(source,sink);
	for(i=0;i<ne;i++) if(from[i]==source && f[i]) {
		/* unmatched letter, reject */
		puts("-1");
		return;
	}
	printf("%d\n",tn-2);
	for(i=0;i<ne;i++) if(!f[i] && from[i]>=letters && from[i]<words && to[i]>=words && to[i]<sink)
		match[from[i]-letters]=to[i]-words;
	for(i=0;i<tn-2;i++) puts(w[include[match[i]]]);
}