随笔分类 - ACM~最短路径
摘要:微软过桥问题的图论解法 微软的过桥问题说的是4个人在晚上过一座小桥,过桥时必须要用到手电筒,只有一枚手电筒,每次最多只可以有两人通过, 4个人的过桥速度分别为1分钟、2分钟、5分钟、10分钟,试问最少需要多长时间4人才可以全部通过小桥?这个问题如果用图论来建模的话,就可以以4个人在桥两端的状态来作为节点来构造一个有向图,如下图所示,以已经过桥了的人的状态作为图的节点,初始时没有人过桥,所以以空表示,第一轮有两个人过桥,有6种可能的组合,(1,2)(1,5)(1,10)(2,5)(2,10)(5,10),从空的状态转换到这些状态的需要的时间分别为2,5,10,5,10,10分钟,时间就作为有向边
阅读全文
摘要:/*两次单源最短路径 输入的时候一次正向记录路径一次反向记录路径 */#include<iostream>#include<cstring>#include<stdio.h>#include<algorithm>using namespace std;#define INF 99999int t[1001][1001],dd[1001][1001];int v[1001],dp[1001],dp2[1001];int n,w;void f(int d[][1001]){ memset(v,0,sizeof(v)); for(int i=1;i&l
阅读全文
摘要:Dijkstra 算法单源最短路径define INF=正无穷w[x][y]表示边不存在(x,y)memset(v,0,sizeof(v));//将所有点标记为没有进入集合for(int i=1;i<=n;i++) d[i]=(i==0?0:INF);//0为源点 到本身到距离为0 其余点到源点到距离为正无穷for(int i=1;i<=n;i++){ int x,m=INF; for(int y=1;y<=n;y++)//选取到源点最的点 if(!v[y]&&d[y]<=m) { m=d[y]; x=y; } v[x]=1;//点x到最短路径已经找到
阅读全文
摘要:Median Weight BeadDescriptionThere are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been perfor.
阅读全文
摘要:Travelling TomTom is a used popsicle salesman. His recent sales at his stand on Gloshaugen haven't been very good, so he's decided that he wants to travel the world selling his merchandise. He has already compiled a list of cities to visit, and have also gathered the costs of the plane trip
阅读全文
摘要:#include<iostream>#include<stdio.h>#include<cstring>using namespace std;#define INF 5000000int a[201];int c[201][201];int d[201][201];int main(){ int n; int i,j,m,kk,k,p; int max1; scanf("%d",&n); int l; for(l=1;l<=n;l++) { memset(a,0,sizeof(a)); memset(c,0,sizeof(
阅读全文

浙公网安备 33010602011771号