hdu2544
!vis[j] && min(d[j])
)拿来刷新到其他点的距离d[v] + mp[v][j] < d[j]
就刷新到j点的距离,j点要是未被刷新的点。(对于邻接矩阵的理解:找于当前的所有(还没有算出最小值的点)里边的最小值,然后再通过找到这个最小值的点去刷新其他(还没有算出最小值)的点的最小值)
(对于表的理解:找当前边里点u的所有点v,也就是v属于u的所有邻接点,然后找到了距离最小的min_v,然后遍历min_v相邻的所有点,刷新最小值。)
void dijkstra(){
for (int i = 0; i < n; i++) d[i] = mp[0][i];
vis[0] = 1;
for (int i = 0; i < n; i++) {
tem = INF;v = 0;
for (int j = 0; j < n; j++)
if (!vis[j] && d[j] <= tem){
tem = d[j];
v = j;
}
vis[v] = 1;
for (int j = 0; j < n; j++)
if (!vis[j] && d[v]+mp[v][j] < d[j])
d[j] = d[v]+mp[v][j];
}
}
hdu1711
//
// main.cpp
// KMP复习
//
// Created by 陈冉飞 on 2020/4/7.
// Copyright © 2020 陈冉飞. All rights reserved.
//
#include
using namespace std;
#define maxn 1000100
int nt[10010],a[maxn],b[10010],T,n,m,cnt;
#include
#define cl(a,b) memset(a,b,sizeof(a))
#include
vector<int> indexVec;
void getnt(){
int i = 0,j = -1;nt[0] = -1;
while (i < n) {
if (j == -1 || b[i] == b[j]) {
i++;j++;nt[i] = j;
}else j = nt[j];
}
}
int kmp(){
getnt();
int i = 0,j = 0;
while (i < m && j < n) {
if (j == -1 || a[i] == b[j]) {
i++;j++;
}else j = nt[j];
if (j == n) {
indexVec.push_back(i-j+1);
cnt++;
// j = 0; //完全匹配
j = nt[j]; //交叉匹配
}
}
// 首次匹配
if (j == n) return i-j+1;
return -1;
}
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
indexVec.clear();
cl(a, 0);cl(b, 0);cnt = 0;
scanf("%d%d",&m,&n);
for (int i = 0; i < m; i++) scanf("%d",&a[i]);
for (int i = 0; i < n; i++) scanf("%d",&b[i]);
// printf("%d\n",kmp());
kmp();
for (int i = 0; i < indexVec.size(); i++) {
printf("%d ",indexVec[i]);
}
// cout<
}
return 0;
}
//13 5
//1 2 1 2 3 1 2 3 1 2 2 1 2
//1 2 3 1 2
!vis[j] && min(d[j])
来作为mst展开的枝节。d[j] > mp[v][j]
(不用再加上d[v]是因为已经在枝节内部)//
// main.cpp
// MST复习
//
// Created by 陈冉飞 on 2020/4/7.
// Copyright © 2020 陈冉飞. All rights reserved.
//
#include
using namespace std;
#define maxn 30
int mp[maxn][maxn],n,m,p,d[maxn],vis[maxn],tem,v,ans;
char s[10],t[10];
#define INF 0x3f3f3f
#include
#define cl(a,b) memset(a,b,sizeof(a))
void prime(){
for (int i = 0; i < n; i++) d[i] = mp[0][i];
for (int i = 1; i < n; i++) {
tem = INF;v = 0;vis[0] = 1;
for (int j = 1; j < n; j++)
if (!vis[j] && tem > d[j])
tem = d[v = j];
vis[v] = 1;ans += tem;
for (int j = 1; j < n; j++)
if (!vis[j] && d[j] > mp[v][j]) d[j] = mp[v][j];
}
printf("%d\n",ans);
}
int main(int argc, const char * argv[]) {
while (~scanf("%d",&n) && n) {
cl(d, 0);cl(vis, 0);ans = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i == j) mp[i][j] = 0;
else mp[i][j] =INF;
for (int i = 0; i < n-1; i++) {
scanf("%s%d",s,&m);
for (int j = 0; j < m; j++) {
scanf("%s%d",t,&p);
mp[s[0]-'A'][t[0]-'A'] = p;
mp[t[0]-'A'][s[0]-'A'] = p;
}
}
prime();
}
return 0;
}
//
// main.cpp
// 并查集复习
//
// Created by 陈冉飞 on 2020/4/7.
// Copyright © 2020 陈冉飞. All rights reserved.
//
#include
using namespace std;
#define maxn 1010
int T,fa[maxn],n,m,p,q;
#include
unordered_set<int> c;
int findfa(int x){
if (x == fa[x]) return x;
return findfa(fa[x]);
}
void merge(int x,int y){
if (findfa(x) != findfa(y)) fa[x] = findfa(y);
}
int main(int argc, const char * argv[]) {
for (scanf("%d",&T); T; T--) {
scanf("%d%d",&n,&m);
for (int i = 0; i < n; i++) fa[i] = i;
for (int i = 0; i < m; i++) {
scanf("%d%d",&p,&q);
merge(p,q);
}
for (int i = 0; i < n; i++)
if (c.find(findfa(i+1)) == c.end())
c.insert(findfa(i+1));
printf("%d\n",c.size());
}
return 0;
}
if (gl <= mid) ans += query(lson, gl, gr);
if (gr >= mid+1) ans += query(rson, gl, gr);
//
// main.cpp
// 线段树复习
//
// Created by 陈冉飞 on 2020/4/7.
// Copyright © 2020 陈冉飞. All rights reserved.
//
#include
using namespace std;
#include
#define cl(a,b) memset(a,b,sizeof(a))
#define maxn 10010
int a[maxn<<2],b[5] = {1,2,3,4,5};
#define lson k<<1,l,mid
#define rson k<<1|1,mid+1,r
// sum
void pushup(int k){a[k] = a[k<<1]+a[k<<1|1];}
void build(int k,int l,int r)
{
if(l==r){a[k] = b[l-1];return;}
int mid = (l+r)/2;
build(lson);build(rson);
pushup(k);
}
int query(int k,int l,int r,int gl,int gr){
if (gl <= l && r <= gr) return a[k];
int mid = (l+r)/2,ans = 0;
if (gl <= mid) ans += query(lson, gl, gr);
if (gr >= mid+1) ans += query(rson, gl, gr);
return ans;
}
void update(int k,int l,int r,int pos,int val){
if (l == r && l == pos) {a[k] = val;return;}
int mid = (l+r)/2;
if (mid >= pos) update(lson, pos, val);
else if (mid+1 <= pos) update(rson, pos, val);
pushup(k);
}
int main(int argc, const char * argv[]) {
build(1, 1, 5);
cout<<query(1, 1, 5, 2, 4)<<endl;
update(1, 1, 5, 3, 1);
cout<<query(1, 1, 5, 2, 4)<<endl;
return 0;
}
//1 2 3 4 5