应用了并查集判环的最小生成树算法
#include <stdio.h>
#include <stdlib.h>

const int LEN = 1005;

int p[LEN];

void init ( int n )
  {

for ( int i=0; i<n; i++ )
 {
p[i] = -1;
}
}

int find ( int a )
  {

if ( p[a] < 0 )
 {

return a;
}

int r = find ( p[a] );
p[a] = r;

return r;
}

void un ( int a, int b )
  {

int ra = find ( a );
int rb = find ( b );

if ( p[ra] < p[rb] )
 {
p[ra] += p[rb];
p[rb] = ra;
}
else
 {
p[rb] += p[ra];
p[ra] = rb;
}
}

struct
  {
int b;
int e;
int len;
}seg[LEN*20];

int cmp ( const void *a, const void *b )
  {

return *( ( int * )b + 2 ) - *( ( int * )a + 2 );
}

int kul ( int n, int m )
  {

qsort ( seg, m, sizeof ( seg[0] ), cmp );

init ( n );
int sum = 0;
for ( int i=0; i<m; i++ )
 {
if ( find ( seg[i].b ) != find ( seg[i].e ) )
 {
sum += seg[i].len;
un ( seg[i].b, seg[i].e );
}
}

for ( i=0; i<n-1; i++ )
 {
if ( find ( i ) != find ( i+1 ) )
 {
return -1;
}
}

return sum;
}

int main ()
  {

int n, m;
int b, e, len;

while ( scanf ( "%d%d", &n, &m ) != EOF )
 {
for ( int i=0; i<m; i++ )
 {
scanf ( "%d%d%d", &b, &e, &len );
seg[i].b = b - 1;
seg[i].e = e - 1;
seg[i].len = len;
}

printf ( "%d\n", kul ( n, m ) );
}
return 0;
}


简单的快排,傻傻的用了自己写的模版,呵呵
#include "stdio.h"

int num[100005];

void swap(int a,int b)
  {
int t;
t=num[a];
num[a]=num[b];
num[b]=t;

}

int partion(int low,int high,int p)
  {
while(low<=high)
 {
if(low<p)
 {
 if(num[low]>num[p]) {swap(low,p);p=low;}
low++;
}
else
 {
if(high>p)
 {
 if(num[high]<num[p]) {swap(high,p);p=high;}
}
high--;
}
}
return p;
}

void qsort(int left,int right)
  {
int middle;
if(left<right)
 {
middle=(left+right)/2;
middle=partion(left,right,middle);
qsort(left,middle-1);
qsort(middle+1,right);
}
}

int main()
  {
int n,k,i,q;
char temp[5];
while(scanf("%d",&n)!=EOF)
 {
for(i=0;i<n;i++)
 {
scanf("%d",&num[i]);
}

scanf("%s",temp);

qsort(0,n-1);

scanf("%d",&k);

for(i=0;i<k;i++)
 {
scanf("%d",&q);

printf("%d\n",num[q-1]);
}
}
return 0;
}

摘要: DFS搜索,是stick的简化版本
#include <stdio.h>#include <stdlib.h>int stick[64];int used[64];int n, m, len;void init ( int n ){  ... 阅读全文
摘要: 线段树和树状数组都可以用
#include <stdio.h>#include <stdlib.h>const int LEN = 15005;//坐标处理struct NODE{ int x; //X坐标 &nbs... 阅读全文
动态规划
#include <stdio.h>

const int LEN = 101;

char hash[26];

int r, c, n;

char map[LEN][LEN];
char cmap[LEN][LEN];

void cpy ()
  {

for ( int i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
map[i][j] = cmap[i][j];
}
}
}

 int move[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };

void ser ()
  {

hash[ 'R'-'A' ] = 'S';
hash[ 'S'-'A' ] = 'P';
hash[ 'P'-'A' ] = 'R';
int flag = 1;
for ( int i=0; i<n; i++ )
 {
flag = 0;
for ( int j=0; j<r; j++ )
 {
for ( int z=0; z<c; z++ )
 {
int tr, tc;
for ( int x =0; x<4; x++ )
 {
tr = j+move[x][0];
tc = z+move[x][1];
if ( tr>=0 && tr<r && tc>=0 && tc<c )
 {
if ( hash[ map[tr][tc]-'A' ] == map[j][z] )
 {
cmap[j][z] = map[tr][tc];
flag = 1;
break;
}
}
}
if ( x >= 4 )
 {
cmap[j][z] = map[j][z];
}
}
}
cpy ();
if ( ! flag )
 {
break;
}
}
}

int main ()
  {

int t;

scanf ( "%d", &t );
while ( t -- )
 {
scanf ( "%d%d%d", &r, &c, &n );

getchar ();
for ( int i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
scanf ( "%c", &map[i][j] );
}
getchar ();
}

ser ();

for ( i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
printf ( "%c", map[i][j] );
}
printf ( "\n" );
}
printf ( "\n" );

}
return 0;
}

广搜题目
#include <stdio.h>

const int LEN = 101;

char hash[26];

int r, c, n;

char map[LEN][LEN];
char cmap[LEN][LEN];

void cpy ()
  {

for ( int i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
map[i][j] = cmap[i][j];
}
}
}

 int move[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };

void ser ()
  {

hash[ 'R'-'A' ] = 'S';
hash[ 'S'-'A' ] = 'P';
hash[ 'P'-'A' ] = 'R';
int flag = 1;
for ( int i=0; i<n; i++ )
 {
flag = 0;
for ( int j=0; j<r; j++ )
 {
for ( int z=0; z<c; z++ )
 {
int tr, tc;
for ( int x =0; x<4; x++ )
 {
tr = j+move[x][0];
tc = z+move[x][1];
if ( tr>=0 && tr<r && tc>=0 && tc<c )
 {
if ( hash[ map[tr][tc]-'A' ] == map[j][z] )
 {
cmap[j][z] = map[tr][tc];
flag = 1;
break;
}
}
}
if ( x >= 4 )
 {
cmap[j][z] = map[j][z];
}
}
}
cpy ();
if ( ! flag )
 {
break;
}
}
}

int main ()
  {

int t;

scanf ( "%d", &t );
while ( t -- )
 {
scanf ( "%d%d%d", &r, &c, &n );

getchar ();
for ( int i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
scanf ( "%c", &map[i][j] );
}
getchar ();
}

ser ();

for ( i=0; i<r; i++ )
 {
for ( int j=0; j<c; j++ )
 {
printf ( "%c", map[i][j] );
}
printf ( "\n" );
}
printf ( "\n" );

}
return 0;
}

摘要: 看题目是字符串处理,但实际上是欧拉回路
#include <stdio.h>#include <stdlib.h>#include <string.h>const int LEN = 30;struct HEAD{ int i... 阅读全文
摘要: 大数运算,用了模
#include<stdio.h>#include<stdlib.h>#include<string.h>const int OneNode = 1000000; //??????????????OneNodeconst int NodeLen = 6;... 阅读全文
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论

阅读排行榜
评论排行榜
|
|