摘要: 经典算法单源最短路:1.Bellman_ford(可判负环,可有负边)d[i]表示起点S到i的最短路,那么d[i]=min{d[j]+w[j][i]}且存在j->i的边代价为w[j][i]经过证明如果不存在负圈最多通过V-1次松弛就可以完成复杂度O(V*E)(V为结点数,E为边数) 1 #inclu... 阅读全文
posted @ 2014-06-08 18:41 默默如潮 阅读(524) 评论(0) 推荐(0) 编辑
摘要: 并查集经典题,但是太坑了,数据只有一组,用EOF读入无限WA,我就这么对着正确的代码查了半个多小时,次奥。把一个结点拆成3个结点分别代表属于A,B,C三类,对于X,Y同类,有如果XA->YA,XB->YB,XC->YC 所以直接把两两并起来,当然要判断能不能并对于X吃Y ,有 XA->YB ,XB-... 阅读全文
posted @ 2014-06-08 17:09 默默如潮 阅读(194) 评论(0) 推荐(0) 编辑
摘要: poj2186Popular Cows分析:直接求一下强连通分量,对于同一个强连通分量里面的结点状态是相同的,要求有多少个人被其他所有的人都认可,只有可能是拓扑排序的最后一个强连通的结点个数,判断一下其他节点是否都可以到该联通分量就ok了。 1 #include 2 #include 3 #in... 阅读全文
posted @ 2014-06-08 01:17 默默如潮 阅读(293) 评论(0) 推荐(0) 编辑
摘要: 题目大意:有个n个m维的东西,要求你把它们垒高,第i个能放在第j个上的要求是对于i,j的所有m维都有a[i][k]=0&&kj边)方程为:dp[i]=max(dp[j]+1)(i->j边存在) 初始为dp[i]=1;路径方案可以通过逆向输出。 1 #include 2 #include 3 #i... 阅读全文
posted @ 2014-04-25 21:49 默默如潮 阅读(399) 评论(0) 推荐(0) 编辑
摘要: hdu 2546题目意思:食堂买菜,只要余额大于5圆就可以买任意的菜,问最少余额为多少。思路:把5圆留着购买最贵的菜,其余的直接0-1背包即可,有点贪心的思想。 1 #include 2 #include 3 #include 4 #include 5 #define maxn 1010 6 #define INF 0x3ffffff 7 using namespace std; 8 int dp[maxn]; 9 int n,m;10 int w[maxn];11 int main ()12 {13 while(scanf("%d",&n),n)14 {15 .. 阅读全文
posted @ 2014-03-17 23:14 默默如潮 阅读(833) 评论(0) 推荐(0) 编辑
摘要: Sereja and Dima1Y直接按着题目给的方法模拟就好了 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #define maxlen 1010 9 using namespace std;10 int n;11 int num[maxlen];12 int main ()13 {14 while(scanf("%d",&n)!=EOF)15 {16 for(int i=0;inum[j])26 {27... 阅读全文
posted @ 2014-01-13 05:10 默默如潮 阅读(391) 评论(0) 推荐(0) 编辑
摘要: 4028The time of a day题目大意:有个奇怪的钟有n(1 2 #include 3 #include 4 #include 5 typedef long long ll; 6 using namespace std; 7 mapdp[50]; 8 int n; 9 ll m;10 ll gcd(ll a,ll b)11 {12 return b==0?a:gcd(b,a%b);13 }14 ll lcm(ll a,ll b)15 {16 return a/gcd(a,b)*b;17 }18 void init()19 {20 dp[1][1]=1;2... 阅读全文
posted @ 2014-01-08 23:17 默默如潮 阅读(357) 评论(0) 推荐(0) 编辑
摘要: 4022Bombing题目大意:给你n个点(二位),坐标范围( >的map x,y表示 横纵有多少个点,每次询问是返回该值就可,然后把对应的另一维的该点删除掉就可以了。 1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 typedef map > m; 8 m x,y; 9 int getans(m &x,m &y,int pos)10 {11 int ans = x[pos].size();12 for(multiset ::iterator i = x[ 阅读全文
posted @ 2014-01-08 22:53 默默如潮 阅读(253) 评论(0) 推荐(0) 编辑
摘要: 402124 Puzzle题目意思:给你一个如上图的图,格子从左到右从上到下编号,每个格子上有数字(0~23,0表示空),操作的过程为当一个格子的上下左右有空格子时可以交换两个格子的数字。现在给你两个序列,表示的是对应编号上的数字,问能不能通过若干次操作将第一种序列变为第二个。分析:首先考虑比较特殊的几个位置即最边上的格子,可以它们只能与一个格子交换所以先预处理一下,把能交换的交换,然后判一下两个序列这些位置上的数字是不是都相同,如果不想同那么最终不可以转化。那么剩下的就是4*4的方格了,其中有一个为空,就是15数码了。根据八数码判断能不能完成我们知道与当前状态的逆序数的奇偶有关。可以发现左右 阅读全文
posted @ 2014-01-08 22:41 默默如潮 阅读(220) 评论(0) 推荐(0) 编辑
摘要: 4027Can you answer these queries?两种操作:1 x y 把[x y]区间中的数变为原来数值的根号(取整)0 x y 求[x y]区间和分析:线段树 区间更新+区间求和 可增加一个标记表示该位置上的数位1或0 因为1 或0 开根号还是本身不用更新 1 #include 2 #include 3 #include 4 #include 5 #include 6 #define maxlen 100010 7 typedef long long ll; 8 using namespace std; 9 struct node10 {11 int l... 阅读全文
posted @ 2014-01-08 12:11 默默如潮 阅读(190) 评论(0) 推荐(0) 编辑