Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

zoj DP(二)

$
0
0

zoj1092

#include <stdio.h>
#include <string.h>
#include <math.h>
#define zero 0.000001

int main()
{
	char name[30][300];
	char from[300], to[300];
	double g[30][30];
	double rate;
	int flag;
	int i,j,k;
	int n,m;
	int cases = 1;
	while(scanf("%d", &n) && n)
	{
		memset(g, 0, sizeof(g));
		for(i=0; i<n; i++)
			scanf("%s", name[i]);
		scanf("%d", &m);
		for(i=0; i<m; i++)
		{
			scanf("%s%lf%s",from, &rate, to);
			for(j=0; j<30; j++)
				if (strcmp(from,name[j])==0) break;
			for(k=0; k<30; k++)
				if (strcmp(to,name[k])==0) break;
			g[j][k] = log(rate);
		}
		for(k=0; k<n; k++)
			for(i=0; i<n; i++)
				for(j=0; j<n; j++)
				{
					rate = g[i][k]+g[k][j];
					if(g[i][j]<rate) g[i][j] = rate;
				}
		flag = 0;
		for(i=0; i<n; i++)
			if (g[i][i]>zero) flag = 1;
		printf("Case %d: ", cases++);
		printf("%s\n", flag ? "Yes" : "No");
	}
	return 0;
}

zoj1117

#include <stdio.h>
#include <string.h>

struct {
	int lchild, rchild, parent;
}tree[64];

char text[100];
int freq[64];
int optimal;

void entropy(int n, int d)
{
	if(freq[n]==0) return;
	if(n<31) optimal += freq[n]*d;
	else
	{
		entropy(tree[n].lchild, ++d);
		entropy(tree[n].rchild, d);
	}
}

int main()
{
	int i,j;
	int left,right;
	while(gets(text))
	{
		if (strcmp(text,"END")==0) break;
		int length = strlen(text);
		memset(tree, 255, sizeof(tree));
		memset(freq, 0, sizeof(freq));
		for(i = 0; i<length; i++)
			if (text[i]=='_') freq[26]++;
			else freq[text[i]-'A']++;
		int node = 30;
		while(1)
		{
			int min = 1000;
			for(j = 0; j<=node; j++)
				if(tree[j].parent==-1)
					if(min>freq[j] && freq[j])
					{
						min = freq[j];
						left = j;
					}
			min = 1000;
			for(j = 0; j<=node; j++)
				if(tree[j].parent==-1)
					if(j!=left && min>freq[j] && freq[j])
					{
						min = freq[j];
						right = j;
					}
			if (min==1000) break;
			freq[++node]  = freq[left] + freq[right];
			tree[node].lchild = left;
			tree[node].rchild = right;
			tree[node].parent = -1;
			tree[left].parent = node;
			tree[right].parent = node;
		}
		optimal = 0;
		if(node==30) optimal = length;
		else entropy(node,0);
		length *= 8;
		printf("%d %d %.1lf\n", length, optimal, 1.0*length/optimal);
	}
	return 0;
}

zoj1118

#include<iostream>
#include<map>
using namespace std;

map<string,int> maps;
int set[200];

int findhead(int number)
{
	if(set[number]==number) return number;
	else
	{
		set[number] = findhead(set[number]);
		return set[number];
	}
}

int build(char path[], int &count)
{
	int index;
	if(maps.find(string(path))==maps.end())
	{
		maps[string(path)] = ++count;
		index = count;
		set[count] = count;
		
	}
	else index = maps[string(path)];
	return index;
}

int main()
{
	int n;
	int number = 1;
	while(scanf("%d",&n) && n)
	{
		int i;
		int p;
		char starting[15], ending[15];
		for(i = 0; i<n; i++)
		{
			scanf("%d", &p);
			starting[i] = p+'0';
		}
		starting[n] = 0;
		for(i = 0; i<n; i++)
		{
			scanf("%d", &p);
			ending[i] = p+'0';
		}
		ending[n] = 0;

		int count = 0;
		maps.clear();
		char path[15];
		while(scanf("%d", &p))
		{
			if(p==-1) break;
			int start, end;
			path[0] = p+'0';
			for(i = 1; i<n; i++)
			{
				scanf("%d", &p);
				path[i] = p+'0';
			}
			path[n] = 0;
			start = build(path, count);			
			for(i = 0; i<n; i++)
			{
				scanf("%d", &p);
				path[i] = p+'0';
			}
			path[n] = 0;
			end = build(path, count);
			int h1 = findhead(start);
			int h2 = findhead(end);
			if(h1!=h2) set[h1] = h2;
		}
		int possible = 1;
		if(maps.find(string(starting))==maps.end() 
			|| maps.find(string(ending))==maps.end())
			possible = 0;
		else
		{
			int start = maps[string(starting)];
			int end = maps[string(ending)];
			int h1 = findhead(start);
			int h2 = findhead(end);
			if(h1!=h2) possible = 0;
		}
		if (possible) printf("Maze #%d can be travelled\n",number++);
		else printf("Maze #%d cannot be travelled\n",number++);
	}
	return 0;
}

zoj1119

#include<stdio.h>
#include<memory.h>


const int Node  = 1000;
int n;
bool G[Node][Node];
int father[Node];
int order[Node];
int low[Node];
bool visited[Node];


void DFS(int v, int d)
{
	if (visited[v]) return;
	low[v] = d;
	order[v] = d;
	visited[v] =  true;
	for (int i = 0; i < n; i++) {
		if (G[v][i]) {
			if (visited[i]) 
				low[v] = low[v]<order[i]?low[v]:order[i];
			else {
				father[i] = v;
				DFS(i, d+1);
				low[v] = low[v]<low[i]?low[v]:low[i];
			}
		}
	}
}


void SPF()
{
	for (int i = 0; i < n; i++) 
	{
		visited[i] = false;
		father[i]  = -1;
		low[i]	 = 0;
		order[i] = 0;
	}
	for (int i = 0; i < n; i++) 
		if (!visited[i]) DFS(i, 1);
	
	bool find = false;
	for (int i = 0; i < n; i++) 
	{
		int subnet = 0;
		for (int j = 0; j < n; j++) 
			if ((father[j] == i) && (low[j] >= order[i])) 
				subnet++;
		if (subnet > 0) {
			if (father[i] == -1) {
				if (subnet > 1) {
					find = true;
					printf("  SPF node %d leaves %d subnets\n", i+1, subnet);
				}
						else {
				find = true;
				printf("  SPF node %d leaves %d subnets\n", i+1, subnet+1);
			}
		}
	}
	if (!find) {
		printf("  No SPF nodes\n");
	}
}


int main(){
	int i,j;
	int number = 0; 
	memset(G, 0, sizeof(G)); 
	while(scanf("%d", &i)) { 
		if(i==0) {
			if(number) putchar('\n'); 
			printf ("Network #%d\n", ++number); 
			SPF();
			scanf("%d", &i);
			if (i==0) break;
			memset(G, 0, sizeof(G)); 
		}
		n = -1; 
		scanf("%d", &j); 
		if(i>n) n = i; 
		if(j>n) n = j; 
		G[i-1][j-1] = G[j-1][i-1]=1; 
	}
}

zoj1127

#include <stdio.h>
#include <memory.h>

int   city;
char  graph [26][26];
char  fortify [26];

int Dijkstra()
{
	int i, j, k;
	int searched [26];
	int hop [26];
	for ( i = 0; i < city; i ++ )
		if ( fortify [i] ) hop [i] = 0;
			else hop [i] = 100;
	memset(searched, 0, sizeof(searched));
	for ( i = 0; i < city; i ++ ) {
		k = -1;
		for ( j = 0; j < city; j ++ )
			if ( !searched [j] ) 
				if ( k < 0 || hop [j] < hop [k] ) k = j;
		if ( k < 0 ) break;
		searched [k] = 1;
		for ( j = 0; j < city; j ++ )
			if (!searched [j])
				if ( graph [k][j] && hop [k] + 1 < hop [j] )
					hop [j] = hop [k] + 1;
	}

	j = -1;
	for ( i = 0; i < city; i ++ )
		if ( !fortify [i] )
			if ( j < 0 || hop [j] < hop [i] ) j = i;
	fortify [j] = 1;
	return j;
}

int main ()
{
	int i;
	int total;
	char first, str[10];
	char a , b;
	scanf("%d%s%d\n", &city, str, &total);
	first = str[0];
	memset (fortify, 0, sizeof(fortify));
	memset (graph, 0, sizeof(graph));
	for ( i = 1; i < city; i ++ ) 
	{
		scanf("%c %c\n", &a, &b);
		graph[a-'A'][b-'A'] = 1;
		graph[b-'A'][a-'A'] = 1;
	}
	fortify [first - 'A'] = 1;
	printf("Program 8 by team X\n");
	printf("%c", first);
	for (i = 1; i < total; i ++ )
		printf(" %c", Dijkstra() + 'A');
	printf("\n");
	printf("End of program 8 by team X\n");
	return 0;
}

zoj1134

#include <stdio.h>
#include <string.h>

#define maxV 1501
int pos[maxV];
int size[maxV]; 
int f1[maxV], f2[maxV];

struct Edge
{		
	int x, y; 
}edge[maxV];

void dp( int now )
{
	f1[now] = 1; 
	f2[now] = 0;
	if (size[now]==0) return; 
	int v;
	int i = pos[now]; 
	while(edge[i].x ==now)
	{
		v = edge[i++].y;
		dp( v );
		f1[now] += f1[v]<f2[v]?f1[v]:f2[v];
		f2[now] += f1[v];
	}
}

int main()
{
	int i, j;
	int u, v, num;
	int node, leaf;
	int V;
	while( scanf("%d", &V )!=EOF )
	{
		int idx = 1; 
		node = leaf = 0;
		for( i=1; i<=V; i++ )
		{
			scanf("%d:(%d)", &u, &num );
			node +=u;
			pos[u] = idx;
			size[u] = num;
			for( j=1; j<=num; j++ )
			{
				scanf("%d", &v );
				leaf +=v;
				edge[idx].x = u; 
				edge[idx++].y = v;
			}
		}
		int root = node-leaf;
		dp(root);
		printf("%d\n", f1[root]<f2[root]?f1[root]:f2[root]);
	}
	return 0;
}

zoj1137

#include<stdio.h>
#include<memory.h>
#define M 500

int n,m;
int match[M];
char used[M];
int g[M][20];

int dfs(int stu)
{
	int i;
	int x, t;
	for(i=1; i<=g[stu][0]; i++)
	{
		x = g[stu][i];
		if(!used[x])
		{
			used[x] = 1;
			t = match[x];
			match[x] = stu;
			if(t==-1||dfs(t)) return 1;
			match[x] = t;
		}
	}
	return 0;
}

int bipartite()
{
	int stu;
	int res = 0;
	memset(match, -1, sizeof(match));
	for(stu=0; stu<n; stu++)
	{
		memset(used, 0, sizeof(used));
		if(dfs(stu)) res++;
	}
	return res;
}

int main()
{
	int i, j;
	int a, b;
	while(scanf("%d",&n)!=EOF)
	{
		memset(g, 0, sizeof(g));
		for(i=0; i<n; i++)
		{
			scanf("%d: (%d)", &a, &m);
			g[a][0] = m;
			for(j=1; j<=m; j++)
			{
				scanf("%d", &b);
				g[a][j] = b;
			}
		}
		printf("%d\n",n-bipartite()/2);
	}
	return 0;
}

zoj1140

#include <stdio.h>
#include <memory.h>

#define MAXN 301
int match[MAXN];
int used[MAXN];
int g[MAXN][120];

int dfs(int course){
	int x, t;
	int i;
	for (i = 1; i<=g[course][0]; ++i){
		x = g[course][i];
		if (!used[x]){
			t = match[x];
			match[x] = course; 
			used[x] = 1;
			if (t == 0 || dfs(t)) return 1;
			match[x] = t;
		}
	}
	return 0;
}
int main(){
	int cases;
	scanf("%d", &cases);
	while (cases--){
		int i, j;
		int n, p;
		scanf("%d%d", &p, &n);
		for (i = 1; i <= p; ++i){
			scanf("%d", &g[i][0]);
			for (j = 1; j<=g[i][0]; ++j) 
				scanf("%d", &g[i][j]);
		}
		memset(match, 0, sizeof(match));
		for (i = 1; i <= p; ++i){
			memset(used, 0, sizeof(used));
			dfs(i);
		}
		int res = 0;
		for (i = 1; i <= n; ++i){
			if (match[i]>0) ++res;
		}
		if (res == p) printf("YES\n"); 
		else printf("NO\n");
	}
	return 0;
}


作者:li771537550 发表于2013-6-4 21:41:18 原文链接
阅读:21 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>