应用了并查集判环的最小生成树算法
#include <stdio.h>
#include <stdlib.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int LEN = 1005;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int p[LEN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void init ( int n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p[i] = -1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int find ( int a )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if ( p[a] < 0 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return a;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int r = find ( p[a] );
p[a] = r;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return r;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void un ( int a, int b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int ra = find ( a );
int rb = find ( b );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if ( p[ra] < p[rb] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p[ra] += p[rb];
p[rb] = ra;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
p[rb] += p[ra];
p[ra] = rb;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int b;
int e;
int len;
}seg[LEN*20];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int cmp ( const void *a, const void *b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return *( ( int * )b + 2 ) - *( ( int * )a + 2 );
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int kul ( int n, int m )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort ( seg, m, sizeof ( seg[0] ), cmp );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
init ( n );
int sum = 0;
for ( int i=0; i<m; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( find ( seg[i].b ) != find ( seg[i].e ) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
sum += seg[i].len;
un ( seg[i].b, seg[i].e );
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( i=0; i<n-1; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( find ( i ) != find ( i+1 ) )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
return -1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return sum;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int n, m;
int b, e, len;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while ( scanf ( "%d%d", &n, &m ) != EOF )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int i=0; i<m; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%d%d%d", &b, &e, &len );
seg[i].b = b - 1;
seg[i].e = e - 1;
seg[i].len = len;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf ( "%d\n", kul ( n, m ) );
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
简单的快排,傻傻的用了自己写的模版,呵呵
#include "stdio.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int num[100005];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void swap(int a,int b)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int t;
t=num[a];
num[a]=num[b];
num[b]=t;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int partion(int low,int high,int p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
while(low<=high)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(low<p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(num[low]>num[p]) {swap(low,p);p=low;}
low++;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(high>p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(num[high]<num[p]) {swap(high,p);p=high;}
}
high--;
}
}
return p;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void qsort(int left,int right)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int middle;
if(left<right)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
middle=(left+right)/2;
middle=partion(left,right,middle);
qsort(left,middle-1);
qsort(middle+1,right);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int n,k,i,q;
char temp[5];
while(scanf("%d",&n)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%d",&num[i]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
scanf("%s",temp);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort(0,n-1);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
scanf("%d",&k);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(i=0;i<k;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%d",&q);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf("%d\n",num[q-1]);
}
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
摘要: 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>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int LEN = 101;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char hash[26];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int r, c, n;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char map[LEN][LEN];
char cmap[LEN][LEN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void cpy ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( int i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
map[i][j] = cmap[i][j];
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int move[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void ser ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
hash[ 'R'-'A' ] = 'S';
hash[ 'S'-'A' ] = 'P';
hash[ 'P'-'A' ] = 'R';
int flag = 1;
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
flag = 0;
for ( int j=0; j<r; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int z=0; z<c; z++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int tr, tc;
for ( int x =0; x<4; x++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tr = j+move[x][0];
tc = z+move[x][1];
if ( tr>=0 && tr<r && tc>=0 && tc<c )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( hash[ map[tr][tc]-'A' ] == map[j][z] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cmap[j][z] = map[tr][tc];
flag = 1;
break;
}
}
}
if ( x >= 4 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cmap[j][z] = map[j][z];
}
}
}
cpy ();
if ( ! flag )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
break;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int t;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
scanf ( "%d", &t );
while ( t -- )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%d%d%d", &r, &c, &n );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
getchar ();
for ( int i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%c", &map[i][j] );
}
getchar ();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ser ();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf ( "%c", map[i][j] );
}
printf ( "\n" );
}
printf ( "\n" );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
广搜题目
#include <stdio.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const int LEN = 101;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char hash[26];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int r, c, n;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char map[LEN][LEN];
char cmap[LEN][LEN];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void cpy ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( int i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
map[i][j] = cmap[i][j];
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int move[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void ser ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
hash[ 'R'-'A' ] = 'S';
hash[ 'S'-'A' ] = 'P';
hash[ 'P'-'A' ] = 'R';
int flag = 1;
for ( int i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
flag = 0;
for ( int j=0; j<r; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int z=0; z<c; z++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
int tr, tc;
for ( int x =0; x<4; x++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tr = j+move[x][0];
tc = z+move[x][1];
if ( tr>=0 && tr<r && tc>=0 && tc<c )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( hash[ map[tr][tc]-'A' ] == map[j][z] )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cmap[j][z] = map[tr][tc];
flag = 1;
break;
}
}
}
if ( x >= 4 )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cmap[j][z] = map[j][z];
}
}
}
cpy ();
if ( ! flag )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
break;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int t;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
scanf ( "%d", &t );
while ( t -- )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%d%d%d", &r, &c, &n );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
getchar ();
for ( int i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%c", &map[i][j] );
}
getchar ();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
ser ();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( i=0; i<r; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( int j=0; j<c; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
printf ( "%c", map[i][j] );
}
printf ( "\n" );
}
printf ( "\n" );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
摘要: 看题目是字符串处理,但实际上是欧拉回路
#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;... 阅读全文
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
28 | 29 | 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 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|