摘要:
最短路径问题,我用的是临接表来存储各个边的信息,用优先队列的dijkstra算法#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn=101;
const int INF=2<<20;
struct edge { int po,w; edge * next;
};
struct node
{ edge * first;
}head[maxn];
typedef pair<int,int>pii;
priori 阅读全文
摘要:
刚开始没有理解题意,以为是最短路径,后来才发现是prim,哎,无语。。。#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=201;
const double INF=2<<20;
double x[maxn],y[maxn],edge[maxn][maxn];
int n;
double getd(int k,int j)
{ double ex=(x[k]-x[j])*(x[k]-x[j]); double ey= 阅读全文
摘要:
单源最短路径,此题主要是要理解题目的意思。根据“他和某个地位较低的人进行了交易,地位较高的的人不会再和他交易,他们认为这样等于是间接接触,反过来也一样。”这句话可知,一定要在一符合要求的区间(这个区间的上限和下限要符合等级限制,并且要包括编号1)里找,然后把所有符合要求的区间的值进行比较,从而得到在限制条件下的最短路径#include <iostream>
using namespace std;
#define maxn 201
#define INF 2<<20
int edge[maxn][maxn];
int inlim[maxn],lev[maxn],val[ 阅读全文
摘要:
最小生成树的基本知识,此图很稠密,所以选用Prim算法要快,Prim O(n2),Kruckal O(elog2e),向此题,e代表边数,远大于n,顶点数,所以选Prim#include <iostream>
using namespace std;
const int maxn=2001;
char s[maxn][7];
int edge[maxn][maxn];
int lit[maxn];
int chan(int a,int b)
{ int amou=0; for(int i=0;i<7;i++) { if(s[a][i]!=s[b][i]) amou++; } 阅读全文
摘要:
trie树水题#include <iostream>
using namespace std;
typedef struct node
{ char data; int count; node * next[26]; node * parent; node() { count=0; memset(next,0,sizeof(next)); }
}trie;
trie * r;
void insert(char * s)//把单词插入trie树中
{ if(r==NULL) r=new trie; trie * p=r; int di; while(*s!='\... 阅读全文
摘要:
优先队列或者说是堆得应用,最暴力的想法就是没输出一个最小值,更新,然后再排序,若用快排,时间效率为O(knlgn),但不需要对所有的排次序,所以就用到最小堆#include <iostream>
#include <queue>
#include <string>
using namespace std;
typedef struct node
{ int n,p,r;
}ar;
bool operator<(ar a,ar b)
{ if(a.p==b.p) return a.n>b.n; else return a.p>b.p;
}
p 阅读全文
摘要:
#include <iostream>
using namespace std;
const int maxn=8000+10;
struct node
{ int l,r,len;
}tree[maxn*3];
void Create(int p,int l,int r)
{ tree[p].l=l; tree[p].r=r; tree[p].len=r-l+1; if(l!=r) { int mid=(l+r)/2; Create(2*p+1,l,mid); Create(2*p+2,mid+1,r); }
}
int find(int p,int am)
{ ... 阅读全文
摘要:
并查集水题#include <iostream>
using namespace std;
const int maxn=50010;
int tot,m,n;
struct node
{ int rank; int data; int parent;
}t[maxn];
void init()
{ for(int i=1;i<=n;i++) { t[i].data=i; t[i].rank=1; t[i].parent=i; }
}
int find(int p)
{ if(p!=t[p].parent) t[p].parent=find(t[p].parent... 阅读全文
摘要:
裸哈希,感觉oj的数据弱啊,如果N很大的话,内存就不够用,可能我没想明白?#include <stdio.h>
#include <string.h>
const int maxn=16000010;
char s[maxn];
int li[300];
short hash[maxn]; int tot; int main()
{ int N,NC; scanf("%d%d",&N,&NC); scanf("%s",s); int sl=strlen(s); int t=1,i,j; memset(hash,0, 阅读全文
摘要:
此题用的是并查集的思路,时间很慢,应该有更好的思路,但以目前的知识只能做成这样#include <iostream>
using namespace std;
const int maxn=10001;
struct UFSTree
{ int data; int parent; int rank; }t[maxn];
int N;
void init()
{ int i; for(i=0;i<N;i++) { t[i].data=i; t[i].parent=i; t[i].rank=1; }
}
void Union(int p,int c)
{ t[c].p... 阅读全文