标签:des out bre ati via tmp 博客 help lang
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 31080 | Accepted: 8486 |
Description
Input
Output
Sample Input
2 2 1 2 5 2 1 4 1 2 2
Sample Output
14
Source
1 #include <iostream> 2 #include <cstdlib> 3 #include <cstring> 4 #include <cstdio> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 9 inline void read(int &x) 10 { 11 x = 0;char ch = getchar(), c = ch; 12 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar(); 13 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar(); 14 if(c == ‘-‘)x = -x; 15 } 16 17 const int MAXN = 50000 + 10; 18 const int MAXM = 500000 + 10; 19 20 struct Edge 21 { 22 int u,v,w,next; 23 Edge(int _u, int _v, int _w, int _next){u = _u;v = _v; w = _w; next = _next;} 24 Edge(){} 25 }edge[MAXM << 2], edge2[MAXM << 2]; 26 int head[MAXN], head2[MAXN], cnt, cnt2; 27 28 inline void insert(int a, int b, int c) 29 { 30 edge[++cnt] = Edge(a,b,c,head[a]); 31 head[a] = cnt; 32 } 33 34 inline void insert2(int a, int b, int c) 35 { 36 edge2[++cnt2] = Edge(a,b,c,head2[a]); 37 head2[a] = cnt2; 38 } 39 40 int n,m,s,t,k; 41 42 int b[MAXN], d[MAXN]; 43 44 struct Node 45 { 46 int p, d; 47 Node(int _p, int _d){p = _p;d = _d;} 48 Node(){} 49 }; 50 51 struct cmp 52 { 53 bool operator()(Node a, Node b) 54 { 55 return a.d > b.d; 56 } 57 }; 58 59 std::priority_queue<Node, std::vector<Node>, cmp> q; 60 61 void dijkstra() 62 { 63 while(q.size())q.pop(); 64 memset(d, 0x3f, sizeof(d)); 65 q.push(Node(t, 0)); 66 d[t] = 0; 67 register Node tmp; 68 while(q.size()) 69 { 70 tmp = q.top(), q.pop(); 71 if(b[tmp.p])continue; 72 b[tmp.p] = 1; 73 for(register int pos = head[tmp.p];pos;pos = edge[pos].next) 74 { 75 int v = edge[pos].v; 76 if(b[v])continue; 77 if(d[v] > tmp.d + edge[pos].w) 78 d[v] = tmp.d + edge[pos].w, q.push(Node(v, d[v])); 79 } 80 } 81 } 82 83 struct cmpp 84 { 85 bool operator()(Node a, Node b) 86 { 87 return a.d + d[a.p] > b.d + d[b.p]; 88 } 89 }; 90 91 std::priority_queue <Node, std::vector<Node>, cmpp> q2; 92 93 int step;//拓展次数 94 95 void abfs() 96 { 97 step = 0; 98 while(q2.size())q2.pop(); 99 q2.push(Node(s, 0)); 100 register Node tmp; 101 if(d[s] == d[0]) 102 { 103 printf("-1\n"); 104 return; 105 } 106 while(q2.size()) 107 { 108 tmp = q2.top(),q2.pop(); 109 if(tmp.p == t) ++ step; 110 if(step == k) 111 { 112 printf("%d\n", tmp.d); 113 return; 114 } 115 for(register int pos = head2[tmp.p];pos;pos = edge2[pos].next) 116 { 117 int v = edge2[pos].v; 118 q2.push(Node(edge2[pos].v, tmp.d + edge2[pos].w)); 119 } 120 } 121 printf("-1\n"); 122 } 123 124 int main() 125 { 126 register int tmp1, tmp2, tmp3; 127 while(scanf("%d %d", &n, &m) != EOF && (n + m)) 128 { 129 memset(b, 0, sizeof(b)); 130 memset(head, 0, sizeof(head)); 131 memset(head2, 0, sizeof(head2)); 132 memset(edge, 0, sizeof(edge)); 133 memset(edge2, 0, sizeof(edge2)); 134 cnt = cnt2 = 0; 135 for(register int i = 1;i <= m;++ i) 136 { 137 read(tmp1), read(tmp2), read(tmp3); 138 insert2(tmp1, tmp2, tmp3); 139 insert(tmp2, tmp1, tmp3); 140 } 141 read(s), read(t), read(k); 142 if(t == s)++ k; 143 dijkstra(); 144 abfs(); 145 } 146 return 0; 147 }
标签:des out bre ati via tmp 博客 help lang
原文地址:http://www.cnblogs.com/huibixiaoxing/p/7468685.html