最短路uva11374

来源:互联网 发布:碎屏幕手机壁纸软件 编辑:程序博客网 时间:2024/05/02 00:06

训练指南上的题。。。。

//此题并没有最优子结构的性质,所以你用动态规划的做法是错的....#include<iostream>#include<cstdio>#include<algorithm>#include<functional>#include<cmath>#include<queue>using namespace std;typedef pair<int, int> p;struct com{bool operator()(p a, p b){return a.first > b.first;}};struct eedge{int to, value;};struct cedge{int to, value;};int efirst[501], cfirst[501],enextt[2050],cnextt[2050];int eedgetot=1, cedgetot=1;eedge eedgee[2050];cedge cedgee[2050];int dist1[501], dist2[501];int tempstart1, tempstart2, top1, top2;int pre1[501], pre2[501];int stack1[501], stack2[501];priority_queue<p, vector<p>, com>que;int  n;void addeedge(int from, int to, int value){eedgee[eedgetot].to = to;eedgee[eedgetot].value = value;enextt[eedgetot] = efirst[from];efirst[from] = eedgetot;eedgetot++;eedgee[eedgetot].to = from;eedgee[eedgetot].value = value;enextt[eedgetot] = efirst[to];efirst[to] = eedgetot;eedgetot++;}void addcedge(int from, int to, int value){cedgee[cedgetot].to = to;cedgee[cedgetot].value = value;cnextt[cedgetot] = cfirst[from];cfirst[from] = cedgetot;cedgetot++;cedgee[cedgetot].to = from;cedgee[cedgetot].value = value;cnextt[cedgetot] = cfirst[to];cfirst[to] = cedgetot;cedgetot++;}void distra(int s,int t,int *dist,int *pre){for (int i = 1; i <= n; i++)dist[i] = 10000000;dist[s] = 0;que.push(p(0, s));while (!que.empty()){p temp = que.top();que.pop();if (dist[temp.second] < temp.first)continue;int num = temp.second; int favalue = temp.first;for (int i = efirst[num]; i; i = enextt[i]){int to = eedgee[i].to;int childvalue = eedgee[i].value;if (dist[to] > favalue+ childvalue){dist[to] = favalue + childvalue;pre[to] = num;que.push(p(dist[to], to));}}}}void solve(int s, int t){distra(s, t, dist1,pre1);distra(t, s, dist2,pre2);int minn = dist1[t];for (int i = 1; i <= n; i++){for (int j = cfirst[i]; j; j = cnextt[j]){int to = cedgee[j].to; int value = cedgee[j].value;if (minn > dist1[i] + value + dist2[to]){minn = dist1[i] + value + dist2[to]; tempstart1 = i; tempstart2 = to;}}}if (!tempstart1){for (int i = t; i; i = pre1[i])stack1[top1++] = i;for (int i = top1 - 1; i >0; i--)printf("%d ", stack1[i]);printf("%d\n", t);printf("Ticket Not Used\n%d\n", minn);}else{for (int i = tempstart1; i; i = pre1[i])stack1[top1++] = i;for (int i = tempstart2; i; i = pre2[i])stack2[top2++] = i;for (int i = top1 - 1; i >= 0; i--)printf("%d ", stack1[i]);for (int i = 0; i < top2 - 1; i++)printf("%d ", stack2[i]);printf("%d\n", t);printf("%d\n%d\n", tempstart1, minn);}}int main(){int  s, e;int casee = 0;while (scanf("%d%d%d", &n, &s, &e) == 3){for (int i = 1; i <= n; i++)cfirst[i] = 0, efirst[i] = 0,pre1[i]=0,pre2[i]=0;cedgetot = 1; eedgetot = 1; top1 = 0; top2 = 0; tempstart1 = 0; tempstart2 = 0;if (casee != 0)printf("\n");casee++;int m, k;scanf("%d", &m);for (int i = 0; i < m; i++){int a, b, c;scanf("%d%d%d", &a, &b, &c);addeedge(a, b, c);}scanf("%d", &k);for (int i = 0; i < k; i++){int a, b, c;scanf("%d%d%d", &a, &b, &c);addcedge(a, b, c);}solve(s, e);}return 0;}