随笔分类 - 图论
摘要:参考 算法导论 p580 单终点最短路径问题这两题是一样的求法,先正着求一次最短路,再将边反向,再求一次矩阵表示图的话就是将矩阵转置一次,邻接表表示的话就是读入边的时候,建两个表poj3268dijkstra求最短路#include <iostream>#include <string.h>using namespace std;const int MAXN =1001;int n,m,x;const int INF = 0x7FFF;int G[MAXN][MAXN];int dist1[MAXN];int sum[MAXN];bool used[MAXN];void
阅读全文
摘要:裸的拓扑排序 图用邻接表表示方法是dfs#include<stdio.h>#include<string.h>const int MAXN = 1000;struct edge{ int u, v, next;}e[MAXN];int n;int first[MAXN], cnt;bool visit[MAXN], f;int sort[MAXN], top;void dfs(int s){ if(visit[s]) return; visit[s] = true; for(int x = first[s]; x != -1; x = e[x].next){ if(!v
阅读全文
摘要:留下代码有空写解题报告 蛋疼的题目#include<stdio.h>const int MAXN = 501;const int INF = 0x3f3f3f3f;int n, m;int map[MAXN][MAXN], dist[MAXN];bool used[MAXN];void dijkstra(){ for(int i = 1; i <= n; ++i){ dist[i] = map[1][i]; used[i] = false; } used[1] = true; dist[1] = 0; for(int i = 1; i < n; ++i){ int mi
阅读全文
摘要:where the sum goes over all pairs of types in the derivation plan such that to is the original type and td the type derived from it and d(to,td) is the distance of the types1/Σ(to,td)d(to,td)派生关系有向边,边权为两个串的不同字符的个数,要求的是找出一种用到所有串的派生关系,使得Q最小,(因为是1/Q),所以这题就是求最小生成树 最朴素的最小生成树AC#include<stdio.h>const
阅读全文
摘要:题意一只母牛从N块田中的任一块(1≤N≤1000)去参加盛大的母牛聚会,这个聚会被安排在X号田(1≤X ≤N)。一共有M(1 ≤ M ≤ 100,000)条单行道分别连接着两块田,且通过路i需要花Ti(1≤Ti≤100)的时间。每头母牛必需参加宴会并且在宴会结束时回到自己的领地,但是每头牛都很懒而喜欢选择化是最少的一个方案。来时的路和去时的可能不一样。求每头牛要来回的最短时间。思路:正着求一次最短路径,矩阵转置,再求一次最短路径,想加求最大#include <iostream>#include <string.h>using namespace std;const in
阅读全文
摘要:dijkstra SSIP#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;const int MAXN = 101;const int MAXINT = 32767;int G[MAXN][MAXN];bool used[MAXN];int pre[MAXN];int dist[MAXN]; void dijkstra(int n){ for(int i = 1; i <= n; i++){ dist[i] = G[1][i]; used[i] =
阅读全文
摘要:给你n个人的联系情况,对任意一个人,求出这个人发消息到其他n-1个人的时间,得到n-1个时间中的最大值,n个最大值中的最小值就是所求。如果网络不通,那就输出disjointFloyed算出任意两个人的最小时间 就OK了#include <iostream>#include <fstream>using namespace std;const int MAXN = 101;const int INF = 0x7FFF;int m , n;int G[MAXN][MAXN];void in(){ int i,j,w,t; for(i = 1; i <= m;i++)
阅读全文
摘要:并查集+set容器 Kruskal#include <stdio.h>#include <string.h>#include <set>using namespace std;const int MAXN = 501;struct E{ int x,y; int weight;};E edge[25001];int father[MAXN];int cmp(const void *d1,const void *d2){ return (*(E*)d1).weight - (*(E*)d2).weight;}void makeSet(int n){ for(i
阅读全文
摘要:基础 Kruskal#include <iostream>#include <stdio.h>using namespace std;const int MAXN = 101;struct E{ int x,y; int weight;};E edge[MAXN*MAXN/2];int G[MAXN][MAXN];int father[MAXN];int cmp(const void *d1, const void *d2){ return (*(E*)d1).weight - (*(E*)d2).weight;}void makeSet(int n){ for(int
阅读全文
摘要:读入边权要注意是否比原来的边权小#include <iostream>#include <fstream>#include <string.h>using namespace std;const int MAXN = 1000;int G[MAXN][MAXN];int lowcost[MAXN];int cloest[MAXN];bool used[MAXN];int cost;void PRIM(int n){ int cnt = 0; cost = 0; used[1] = true; for(int i = 2; i <= n; i++){ l
阅读全文
摘要:最小生成树..#include <iostream>#include <string.h>#include <math.h>#include <iomanip>using namespace std;const int MAXN = 101;struct point { double x,y;};point p[MAXN];double G[MAXN][MAXN];double lowcost[MAXN];int closest[MAXN];bool used[MAXN];double cost;void PRIM(int n){ cost =
阅读全文
摘要:基础的最小生成树 有的路已经建好了,就把矩阵置0 就可以了//prim MST#include <stdio.h>#include <stdlib.h>#include <string.h>int G[101][101]; bool used[101]; int lowcost[101];int closest[101];int cost;void prim(int n){ cost = 0; used[1] = true; int minj; for(int i = 2; i <= n; i++){ lowcost[i] = G[i][1]; clo
阅读全文
摘要:并查集 找连通分量 水之#include <stdio.h>int set[1001];int find(int x){ while(set[x] != x) x = set[x]; return x;}void merge(int x,int y){ int fx , fy; fx = find(x); fy = find(y); if(fx != fy){ set[fx] = fy; }}int main(){ int m,n,i,cnt,x,y; while(scanf("%d",&n),n){ for(i = 1; i <= 1000; +
阅读全文

浙公网安备 33010602011771号