摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2647拓扑排序方法如下:(1)从有向图中选择一个没有前驱(即入度为0)的顶点并且输出它.(2)从网中删去该顶点,并且删去从该顶点发出的全部有向边.(3)重复上述两步,直到剩余的网中不再存在没有前趋的顶点为止.第一次做拓扑排序,有几点要说明一下:1.vector是一个容器,可以动态分配内存,存储方式是线性的,他有几个常用的函数:s[b].push_back(a):把a放到b这一数组后面;s[b].size():测量在s[b]后面放了几个元素。2.pair中的make_pair(x,y)可以把两个数据组合成 阅读全文
posted @ 2013-08-13 17:02 小の泽 阅读(246) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2094产生冠军的前提是有且只有一个人赢(貌似是废话)......源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 char temp1[30],temp2[30]; 6 char topo[2005][30]; 7 int win[2005];//记录输赢,-1表示赢,0表示输 8 int main() 9 {10 // freopen("in.txt","r",stdin);11 //f 阅读全文
posted @ 2013-08-12 17:21 小の泽 阅读(143) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2084这题如果从上往下递推就要分两种情况写递推式:if(i-j>=1)dp[i][j]=max(dp[i-1][j]+dp[i-1][j+1])+当前[i][j]的值elsedp[i][j]=dp[i-1][j-1]+当前[i][j]的值但是如果倒过来考虑,也就是从底部开始往上递推,那么递推式就是:dp[i][j]=max(dp[i+1][j],dp[i+1][j+1])+当前[i][j]的值这样写就方便多了。源代码: 1 #include 2 #include 3 #include 4 usin 阅读全文
posted @ 2013-08-12 12:13 小の泽 阅读(163) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875最小生成树,用Prim算法。源代码: 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 const double INF=100000000.0; 8 int visit[110]; 9 double d[110],w[110][110];10 struct11 {12 int x,y;13 } re[110];14 15 int main()16 {17 int n,c... 阅读全文
posted @ 2013-08-09 21:20 小の泽 阅读(157) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=2122注意考虑只有一个城市的情况!!!还有题目有点问题,应该是接下来一共m行~~~赤果果的最小生成树,用Kruskal。源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 typedef struct{ 6 int from,to,c; 7 }road; 8 road r[10000010]; 9 int fa[1010];10 int count;11 12 int cmp(const void *a,const void... 阅读全文
posted @ 2013-08-09 15:31 小の泽 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102这题有多组测试数据,表示被坑了……最小生成树,用Kruskal~源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 typedef struct{ 6 int from,to,c; 7 }road; 8 road r[250010]; 9 int fa[510];10 int count;11 12 int cmp(const void *a,const void *b){13 return (((road *)a... 阅读全文
posted @ 2013-08-09 14:31 小の泽 阅读(172) 评论(0) 推荐(0) 编辑
摘要: 题目:http://poj.org/problem?id=2485因为让你求最长边,所以首先想到的是Kruskal算法(他加入生成树的最后一条边是最大边),注意n个城市有n-1条边!!!源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 typedef struct 6 { 7 int from,to,c; 8 } road; 9 road r[250010];10 int fa[510];11 int count;12 13 int cmp(const void *a,const void *b)14 {15... 阅读全文
posted @ 2013-08-09 13:35 小の泽 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371最小生成树,这题用Kruskal算法加并查集,对于已经连通的集合处理方式是把他们的根结点设为同一个。注意边的添加不能使最小生成树产生回路。还有只能用C++交,用G++会超时啊~~~orz源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 //const int INF=100000000; 6 const int maxn=255003; 7 int road,ans,i,j,n,m,k; 8 int from[maxn]. 阅读全文
posted @ 2013-08-09 11:33 小の泽 阅读(153) 评论(0) 推荐(0) 编辑
摘要: 题目:http://poj.org/problem?id=1321八皇后问题,用dfsj即可。源代码: 1 #include 2 #include 3 #include 4 using namespace std; 5 int a[10][10]; 6 int visit[10]; 7 int n,k,sum; 8 __int64 ans; 9 10 void dfs(int x){11 int i,j;12 for(i=x+1;i<n;i++)13 for(j=0;j<n;j++){14 if(a[i][j]&&visit[j]==0){15 visit[j... 阅读全文
posted @ 2013-08-08 14:37 小の泽 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3790用Dijstra,考虑重边的情况。源代码: 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 typedef struct{ 7 int di,p;}road; 8 road w[1005][1005],d[1005]; 9 int visit[1005];10 const int INF=100000000;11 int main()12 {13 int n,m,i,j,s,e,ok,temp_1,... 阅读全文
posted @ 2013-08-07 18:39 小の泽 阅读(150) 评论(0) 推荐(0) 编辑