HDU 1247 Hat’s Words
摘要:Trie树 + 暴力。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 6 using namespace std; 7 8 char s[50002][100]; 9 10 typedef struct node 11 { 12 int flag; 13 node *next[26]; 14 }node; 15 16 node *newnode() //新节点 17 { 18 int i; 19 node *...
阅读全文
posted @
2012-08-14 14:20
zrq495
阅读(191)
推荐(0)
HDU 1003 Max Sum
摘要:DP。代码如下: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 5 using namespace std; 6 7 int main() 8 { 9 int T, n, i, a, sum;10 int k, t, x, y, maxn;11 cin >> T;12 for (k=1; k<=T; k++)13 {14 t=1;15 sum=0;16 maxn=-1000;17 cin >> n;18 ...
阅读全文
posted @
2012-08-10 19:29
zrq495
阅读(126)
推荐(0)
HDU 1754 I Hate It
摘要:线段树。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #define max 2000002 4 5 using namespace std; 6 7 typedef struct 8 { 9 int l, r;10 int val;11 }node;12 13 node t[4*max];14 15 void build(int l, int r, int rt)16 {17 t[rt].l=l;18 t[rt].r=r;19 if (l == r)20 {21 scanf(...
阅读全文
posted @
2012-08-09 21:15
zrq495
阅读(153)
推荐(0)
HDU 1166 敌兵布阵
摘要:线段树。代码如下: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #define max 50002 5 6 using namespace std; 7 8 typedef struct 9 {10 int l, r;11 int val;12 }node;13 14 node w[4*max];15 16 void pushup(int rt)17 {18 w[rt].val=w[rt<<1].val+w[rt<<1|1].val;19 }20 21 v
阅读全文
posted @
2012-08-09 21:14
zrq495
阅读(175)
推荐(0)
HDU 1874 畅通工程续
摘要:最短路.dijkstra 算法.代码如下: 1 #include<iostream> 2 #include<cstring> 3 #define MAX 9999999 4 5 using namespace std; 6 7 int n, m; 8 int map[202][202]; 9 int vis[202]; //标记是否更新 1为更新10 int dir[202]; //到出发点的距离11 12 void dij(int s)13 {14 int i, j;15 int pos, min;16 memset(vis, 0, sizeof(v...
阅读全文
posted @
2012-08-07 16:13
zrq495
阅读(437)
推荐(0)
HDU 1102 Constructing Roads
摘要:最小生成树。prim算法。要问while(scanf("%d", &n)) 和 while(~scanf("%d", &n))、while(scanf("%d", &n)==1)、while(~scanf("%d", &n)!=EOF) 的区别有多大??答 :前者TLE ,后者 15MS。。。。 不信可以试试。。以后再也不敢简写了。。。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #include<cst
阅读全文
posted @
2012-08-06 21:23
zrq495
阅读(175)
推荐(0)
HDU 1863 畅通工程
摘要:最小生成树。Prim 算法。代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int map[102][102]; 8 9 int prim(int n)10 {11 int i, j, min, sum=0;12 int pos, vis[102], d[102];13 memset(vis, 0, sizeof(vis));14 for (i=1; i<=n; i++)15 d[i]=map[1][i]...
阅读全文
posted @
2012-08-06 20:10
zrq495
阅读(188)
推荐(0)
HDU 1879 继续畅通工程
摘要:最小生成树.Kruskal 算法代码: 1 #include<iostream> 2 #include<cstdlib> 3 #include<cstdio> 4 5 using namespace std; 6 7 typedef struct 8 { 9 int a, b;10 int d;11 int flag;12 }node;13 14 int set[102];15 node w[5002];16 17 int cmp(const void *a, const void *b)18 {19 node *pa=(node *)a;20 no...
阅读全文
posted @
2012-08-06 20:08
zrq495
阅读(111)
推荐(0)
最小生成树
摘要:给定一个带权的无向连通图,如何选取一棵生成树,使树上所有边上权的总和为最小,这叫最小生成树.求最小生成树的算法(1)克鲁斯卡尔算法图的存贮结构采用边集数组,且权值相等的边在数组中排列次序可以是任意的.该方法对于边相对比较多的不是很实用,浪费时间.(2)普里姆算法图的存贮结构采用邻接矩阵.此方法是按各个顶点连通的步骤进行,需要用一个顶点集合,开始为空集,以后将以连通的顶点陆续加入到集合中,全部顶点加入集合后就得到所需的最小生成树 .下面来具体讲下:克鲁斯卡尔算法方法:将图中边按其权值由小到大的次序顺序选取,若选边后不形成回路,则保留作为一条边,若形成回路则除去.依次选够(n-1)条边,即得最小生
阅读全文
posted @
2012-08-06 20:05
zrq495
阅读(168)
推荐(0)
HDU 1233 还是畅通工程
摘要:最小生成树。Kruskal 算法:其中应用并查集。 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 5 using namespace std; 6 7 typedef struct 8 { 9 int a, b;10 int d;11 }node;12 13 node w[5002];14 int set[102];15 16 int cmp(const void *a, const void *b)17 {18 node *x=(node *)a;19 node *y=(node
阅读全文
posted @
2012-08-06 19:57
zrq495
阅读(399)
推荐(0)
UVa 568 - Just the Facts
摘要:WA 了好几次,只保留一位数不够。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 long long a[10001]; 7 8 int main() 9 {10 long long n, i, t;11 a[0]=1;12 for (i=1; i<=10000; i++)13 {14 t=a[i-1]*i;15 while(t%10 == 0)16 t/=10;17 a[i]=t%1...
阅读全文
posted @
2012-08-05 21:44
zrq495
阅读(186)
推荐(0)
HDU 2674 N!Again
摘要:2009=287*7=41*49=41*7*7。所以当n>=41时,n!%2009=0。n<41时,用公式:(a*b)%c = ((a%c) * (b%c)) % c。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 long long n, i ,s; 8 while(cin >> n) 9 {10 if (n == 0)11 {12 cout << "1" << endl;13 conti...
阅读全文
posted @
2012-08-04 21:33
zrq495
阅读(181)
推荐(0)
HDU 1213 How Many Tables
摘要:并查集简单应用。和HDU 1232 一样。代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int set[1010]; 6 int cnt; 7 8 int find(int a) 9 {10 int x=a;11 while(x != set[x])12 x=set[x];13 return x;14 }15 16 void merge(int a, int b)17 {18 int x=find(a);19 int y=find(b);20 if (x!=y...
阅读全文
posted @
2012-08-04 21:19
zrq495
阅读(147)
推荐(0)
HDU 1071 The area
摘要:题意:给出三个点,求抛物线和直线所围成的面积。设抛物线的方程为y=a(x-h)^2+k, 直线方程为y=dx+e; 所以h和k分别是顶点的横纵坐标,即h=x1,k=y1.把(x2,y2)或(x3,y3)代入抛物线方程求出 a,代入直线方程求出d和e 。(见代码)然后分别对它们积分,求出面积,相减即可。代码如下: 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int T; 9 double s1, s2;10 double a, h, k, e..
阅读全文
posted @
2012-08-04 21:11
zrq495
阅读(165)
推荐(0)
HDU 1272 小希的迷宫
摘要:并查集。781MS,水过。。。代码: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int set[100002], mark[100002]; 7 int flag; 8 9 int find(int a) // 查找a的根结点10 {11 int x=a;12 while(set[x] != x)13 x=set[x];14 return x;15 }16 17 void merge(int a, int b) //合并a和b都的根结点18 {19...
阅读全文
posted @
2012-08-03 15:32
zrq495
阅读(120)
推荐(0)
HDU 1232 畅通工程
摘要:并查集算法。 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int cnt; //统计最少要添加的道路 7 int set[1001]; 8 9 int find(int a) //查找a的根结点10 {11 int x=a;12 while(set[x] != x)13 x=set[x];14 return x;15 }16 17 void merge(int a, int b) //合并a和b的根结点18 {19 int x...
阅读全文
posted @
2012-08-03 11:14
zrq495
阅读(124)
推荐(0)
HDU 2094 产生冠军
摘要:判断顶点入度是否唯一即可。如果入度为0的节点只有一个,输出Yes,否则输出No。代码: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int len; 7 int map[1001][1001]; 8 char name[1001][100]; 9 10 int func(char str[])11 {12 int i;13 for (i=0; i<len; i++)14 {15 if (strcmp(name[i], str)==0)16 ...
阅读全文
posted @
2012-08-02 18:41
zrq495
阅读(159)
推荐(0)
HDU 1285 确定比赛名次
摘要:拓扑排序。 (1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它. (2)从网中删去该顶点,并且删去从该顶点发出的全部有向边. (3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.代码如下: 1 #include<iostream> 2 #include<cstring> 3 4 using namespace std; 5 6 int main() 7 { 8 int i, j, k, n, m, a, b; 9 int dgr[501], map[501][501]; //dgr[]存放节点的入度10 while(ci...
阅读全文
posted @
2012-08-02 18:23
zrq495
阅读(461)
推荐(0)
HDU 1181 变形课
摘要:map[i][j]=1 表示第i 个顶点指向第j个顶点;如果map[1][i]=1, map[i][j]=1, 则map[1][j]=1。代码如下;#include<iostream>#include<cstring>using namespace std;int map[100][100];int main(){ int i, j; char str[1000]; while(cin >> str) { while (strcmp(str, "0")) { int len=strlen(str); ...
阅读全文
posted @
2012-08-01 21:08
zrq495
阅读(180)
推荐(0)
HDU 1241 Oil Deposits
摘要:DFS代码如下: 1 #include<iostream> 2 3 using namespace std; 4 5 int m, n; 6 char map[102][102]; 7 int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}}; //标记8个方向 8 9 void dfs(int x, int y)10 {11 int i, xx, yy;12 for (i=0; i<8; i++) //扫描8个方向13 {14 ...
阅读全文
posted @
2012-08-01 11:05
zrq495
阅读(230)
推荐(0)