hdu 3790 最短路径问题(打印路径)
2016-05-27 21:00
417 查看
int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } typedef long long llt ; #define MOD 1000000007 const int maxn = 150000; #define LEFT 0 #define RIGHT 1 //cout.sync_with_stdio(false); /// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。 //freopen("cin.txt", "r", stdin); //freopen("cout.txt", "w", stdout); struct weight_t{ int cost = INT_MAX; int leight = INT_MAX; weight_t(int l = 0,int c = 0):leight(l),cost(c){} friend bool operator <(weight_t const &a,weight_t const &b){ if ( a.leight == b.leight ) return a.cost < b.cost; return a.leight < b.leight; } void clr(){ cost = INT_MAX; leight = INT_MAX; } friend bool operator == (weight_t const &a,weight_t const &b){ return (a.leight == b.leight)&&(a.cost == b.cost); } friend weight_t operator +(weight_t const &a,weight_t const &b){ weight_t temp; temp.cost = a.cost + b.cost; temp.leight = a.leight + b.leight; return temp; } bool isMax(){ return (cost == INT_MAX) &&(leight == INT_MAX); } }; //typedef int weight_t; struct edge_t{ int v; weight_t w; edge_t *next; }Edge[100010]; edge_t *V[1050]; int n; int ECnt = 0; void mkEdge(int const &a,int const &b,weight_t const &w){ Edge[ECnt].v = b; Edge[ECnt].w = w; Edge[ECnt].next = V[a]; V[a] = Edge+ECnt++; Edge[ECnt].v = a; Edge[ECnt].w = w; Edge[ECnt].next = V[b]; V[b] = Edge+ECnt++; } weight_t D[1050]; bool flag[1050]; void spfa(int s){ MEM(flag,0); for (int i = 0;i <= n;++i){ D[i].clr(); } D[s].cost = D[s].leight = 0; flag[s] = true; queue<int>q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); flag[u] = false; for(edge_t *p = V[u];p;p = p->next){ int v = p->v; weight_t temp = D[u] + p->w; if (temp < D[v]){ D[v] = temp; if (!flag[v]){ flag[v] = true; q.push(v); } } } } } void init(){ ECnt = 0; memset(V,0,sizeof(V)); MEM(Edge,0); } int m; int s,t; bool read(){ init(); scanf("%d%d",&n,&m); if (n == 0&& m == 0) return false; int a,b,l,c; for (int i = 0;i < m;++i){ scanf("%d%d%d%d",&a,&b,&l,&c); weight_t w(l,c); mkEdge(a,b,w); } scanf("%d%d",&s,&t); return true; } int main(){ while(read()){ spfa(s); printf("%d %d\n",D[t].leight,D[t].cost); } return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析