来个图论,http://acm.hdu.edu.cn/showproblem.php?pid=2112
2013-04-17 20:58
441 查看
这几天准备看图论,从最基本的最短路开始,建图是基础知识,网上搜了这么个题,其实昨天就过了,不过我想用两种方法写,邻接矩阵很熟悉了,但是邻接表不怎么熟悉,迪杰斯特拉算法的思路掌握了,但是邻接矩阵的代码是参考的模版,于是自己写了一个邻接表的,自己打出来感觉对算法的掌握理解更好,也容易记住代码。
以下第一个是邻接矩阵版本,第二个是邻接表。特意把两个代码改的比较相似,交到HDU看了一下,时间基本一样,还没有发现各自的优势,据说邻接表在稀疏的时候很牛叉,以后再说。
思路就不说了,经典算法,不会的去面壁。。。
准备明天再把这个题写成优先队列优化的迪杰斯特拉,再用其他几种最短路算法写一遍,让这个题变成一个典型例题,好好总结一下。
顺便这个题能学习一下map的用法,以前会一点,但是不太熟,感觉java的map比较好用,但是和队友比赛最好大家都用c,不然代码不好交流。
以下第一个是邻接矩阵版本,第二个是邻接表。特意把两个代码改的比较相似,交到HDU看了一下,时间基本一样,还没有发现各自的优势,据说邻接表在稀疏的时候很牛叉,以后再说。
思路就不说了,经典算法,不会的去面壁。。。
准备明天再把这个题写成优先队列优化的迪杰斯特拉,再用其他几种最短路算法写一遍,让这个题变成一个典型例题,好好总结一下。
顺便这个题能学习一下map的用法,以前会一点,但是不太熟,感觉java的map比较好用,但是和队友比赛最好大家都用c,不然代码不好交流。
#include "iostream" #include "stdlib.h" #include "algorithm" #include "cmath" #include "cstring" #include "cstdio" #include "map" using namespace std; #define INF 999999999 int n,cnt,flag,a,b,c; char name1[40],name2[40]; int str[160][160],dis[160]; bool vis[160]; map<string,int> m; void init() { m.clear(); cnt=0; flag=1; for(int i=0; i<160; i++) { dis[i]=INF; for(int j=0; j<160; j++) str[i][j]=INF; } memset(vis,false,sizeof(vis)); } void djst() { dis[0]=0; int pos=0; for(int i=0; i<cnt; i++) { vis[pos]=true; for(int j=0; j<cnt; j++) if(str[pos][j]!=INF&&(dis[pos]+str[pos][j]<dis[j])) dis[j]=dis[pos]+str[pos][j]; pos=-1; for(int j=0; j<cnt; j++) { if(!vis[j]&&(pos==-1||dis[j]<dis[pos])) pos=j; } } } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { if(n==-1) break; init(); scanf("%s %s",name1,name2); if(strcmp(name1,name2)==0) flag=0; m.insert(make_pair(name1,cnt++)); m.insert(make_pair(name2,cnt++)); for(int i=0; i<n; i++) { scanf("%s %s %d",name1,name2,&c); if(m.find(name1)==m.end()) m.insert(make_pair(name1,cnt++)); if(m.find(name2)==m.end()) m.insert(make_pair(name2,cnt++)); a=m[name1]; b=m[name2]; str[a][b]=min(str[a][b],c); str[b][a]=min(str[b][a],c); } if(flag==0) printf("0\n"); else { djst(); if(dis[1]==INF) printf("-1\n"); else printf("%d\n",dis[1]); } } return 0; }
#include "iostream" #include "stdlib.h" #include "algorithm" #include "cmath" #include "cstring" #include "cstdio" #include "map" using namespace std; #define INF 999999999 #define MAXN 10010 int n,flag,a,b,c; char name1[40],name2[40]; int dis[160]; bool vis[160]; int cnt,point;//边数,点数 int lastshow[160]; map<string,int> m; struct edge { int to,next,weight; } e[2*MAXN]; void insert(int a,int b,int weight) { e[cnt].to=b; e[cnt].next=lastshow[a]; e[cnt].weight=weight; lastshow[a]=cnt++; } void init() { cnt=0; point=0; flag=1; m.clear(); memset(lastshow,-1,sizeof(lastshow)); memset(vis,false,sizeof(vis)); for(int i=0; i<160; i++) dis[i]=INF; } void djst() { dis[0]=0; int pos=0; for(int i=0; i<point; i++) { vis[pos]=true; int t=lastshow[pos]; while(t!=-1) { int to=e[t].to; dis[to]= min(dis[to],dis[pos]+e[t].weight); t=e[t].next; } pos=-1; for(int j=0; j<point; j++) { if(!vis[j]&&(pos==-1||dis[j]<dis[pos])) pos=j; } } } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { if(n==-1) break; init(); scanf("%s %s",name1,name2); if(strcmp(name1,name2)==0) flag=0; m.insert(make_pair(name1,point++)); m.insert(make_pair(name2,point++)); for(int i=0; i<n; i++) { scanf("%s %s %d",name1,name2,&c); if(m.find(name1)==m.end()) m.insert(make_pair(name1,point++)); if(m.find(name2)==m.end()) m.insert(make_pair(name2,point++)); a=m[name1]; b=m[name2]; insert(a,b,c); insert(b,a,c); } if(flag==0) printf("0\n"); else { djst(); if(dis[1]==INF) printf("-1\n"); else printf("%d\n",dis[1]); } } return 0; }水平有限,众神轻喷。
相关文章推荐
- HDU Today&&http://acm.hdu.edu.cn/showproblem.php?pid=2112
- http://acm.hdu.edu.cn/showproblem.php?pid=2112
- 士兵队列训练问题http://acm.hdu.edu.cn/showproblem.php?pid=1276
- To The Max &&http://acm.hdu.edu.cn/showproblem.php?pid=1081
- http://acm.hdu.edu.cn/showproblem.php?pid=1527..
- Encoding http://acm.hdu.edu.cn/showproblem.php?pid=1020
- http://acm.hdu.edu.cn/showproblem.php?pid=1059&&多重背包
- HDU 1702 http://acm.hdu.edu.cn/showproblem.php?pid=1702
- http://acm.hdu.edu.cn/showproblem.php?pid=1686
- http://acm.hdu.edu.cn/showproblem.php?pid=3371
- http://acm.hdu.edu.cn/showproblem.php?pid=1299 (求因子个数)
- http://acm.hdu.edu.cn/showproblem.php?pid=1568
- http://acm.hdu.edu.cn/showproblem.php?pid=1722
- http://acm.hdu.edu.cn/showproblem.php?pid=1754 更新节点,区间最值
- http://acm.hdu.edu.cn/showproblem.php?pid=1255 扫描线 + 线段树 +离散化
- HDU 1455 http://acm.hdu.edu.cn/showproblem.php?pid=1455
- http://acm.hdu.edu.cn/showproblem.php?pid=2544
- http://acm.hdu.edu.cn/showproblem.php?pid=1087
- http://acm.hdu.edu.cn/showproblem.php?pid=2110
- http://acm.hdu.edu.cn/showproblem.php?pid=2674 (很神奇)