标签:its namespace range nod cstring turn des determine eof
Description
Input
Output
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
理解dijkstra,明天写完
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 #include <vector> 7 using namespace std; 8 #define inf 0x3f3f3f3f 9 const int maxn =1010; 10 vector <int>G[maxn]; 11 bool done[maxn]; 12 int p[maxn]; 13 int d[maxn]; 14 int t,n; 15 struct Edge{ 16 int from,to,dis; 17 Edge(int u,int v,int d):from(u),to(v),dis(d){} 18 }; 19 vector <Edge>edges; 20 struct HeapNode { 21 int d,u; 22 bool operator <(const HeapNode& x) const { 23 return d>x.d; 24 } 25 }; 26 void addEdge(int u,int v,int dis){ 27 edges.push_back(Edge(u,v,dis)); 28 int m=edges.size(); 29 G[u].push_back(m-1); 30 } 31 void dijkstra (int s) { 32 priority_queue<HeapNode> q; 33 memset(d,inf,sizeof d); 34 d[s]=0; 35 memset(done,false,sizeof done); 36 q.push(HeapNode{0,s}); 37 while (!q.empty()){ 38 HeapNode x=q.top(); 39 q.pop(); 40 int u=x.u; 41 if (done[u]) 42 continue; 43 done[u]=true; 44 for (int i=0;i<G[u].size();++i){ 45 Edge& e=edges[G[u][i]]; 46 if (d[e.to]>d[u]+e.dis){ 47 d[e.to]=d[u]+e.dis; 48 p[e.to]=G[u][i]; 49 q.push((HeapNode){d[e.to],e.to}); 50 } 51 } 52 } 53 } 54 void init(){ 55 for (int i=0;i<n;++i) 56 G[i].clear(); 57 edges.clear(); 58 } 59 int main() 60 { 61 //freopen("de.txt","r",stdin); 62 while (~scanf("%d%d",&t,&n)){ 63 init(); 64 for (int i=0;i<t;++i){ 65 int x,y,z; 66 scanf("%d%d%d",&x,&y,&z); 67 addEdge(x,y,z); 68 addEdge(y,x,z); 69 } 70 dijkstra(1); 71 printf("%d\n",d[n]); 72 } 73 return 0; 74 }
POJ 2387 Til the Cows Come Home (dijkstra模板题)
标签:its namespace range nod cstring turn des determine eof
原文地址:http://www.cnblogs.com/agenthtb/p/6072164.html