标签:man sea pst std etc connect really required ice
Remmarguts‘ Date
Description
Input
Output
Sample Input
2 2
1 2 5
2 1 4
1 2 2
Sample Output
14
Source
1 #include <queue> 2 #include <cstdio> 3 #include <ctype.h> 4 #include <cstring> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int MAXM=100010; 10 const int MAXN=1010; 11 const int INF=0x7fffffff; 12 13 struct edge { 14 int to; 15 int next; 16 int val; 17 edge() {} 18 edge(int to,int val,int next):to(to),val(val),next(next) {} 19 }; 20 edge e[MAXM<<1],r[MAXM<<1]; 21 22 int head[MAXN],Head[MAXN],tot; 23 24 int dis[MAXN]; 25 26 int n,m,S,T,k,inr; 27 28 bool vis[MAXN]; 29 30 struct data { 31 int to; 32 int dist; 33 bool operator < (data p) const { 34 return dist+dis[to]>dis[p.to]+p.dist; 35 } 36 }; 37 data s; 38 39 inline void read(int&x) { 40 int f=1;register char c=getchar(); 41 for(x=0;!isdigit(c);c==‘-‘&&(f=-1),c=getchar()); 42 for(;isdigit(c);x=x*10+c-48,c=getchar()); 43 x=x*f; 44 } 45 46 inline void add(int x,int y,int v) { 47 e[++tot]=edge(y,v,head[x]); 48 r[tot]=edge(x,v,Head[y]); 49 head[x]=Head[y]=tot; 50 } 51 52 inline void init() { 53 memset(head,-1,sizeof head); 54 memset(Head,-1,sizeof Head); 55 tot=0; 56 } 57 58 void spfa() { 59 queue<int> Q; 60 for(int i=1;i<=n;++i) dis[i]=INF,vis[i]=false; 61 Q.push(T); 62 dis[T]=0; 63 vis[T]=true; 64 while(!Q.empty()) { 65 int now=Q.front(); 66 Q.pop(); 67 vis[now]=false; 68 for(int i=Head[now];i!=-1;i=r[i].next) { 69 int to=r[i].to; 70 if(dis[to]>dis[now]+r[i].val) { 71 dis[to]=dis[now]+r[i].val; 72 if(!vis[to]) vis[to]=true,Q.push(to); 73 } 74 } 75 } 76 return; 77 } 78 79 int Astar() { 80 inr=0; 81 if(dis[S]==-1) return -1; 82 if(S==T) ++k; 83 priority_queue<data> Q; 84 s.to=S;s.dist=0; 85 Q.push(s); 86 while(!Q.empty()) { 87 data now=Q.top(); 88 Q.pop(); 89 if(now.to==T) ++inr; 90 if(inr==k) return now.dist; 91 for(int i=head[now.to];i!=-1;i=e[i].next) { 92 int to=e[i].to; 93 data LC=now; 94 LC.to=to;LC.dist=now.dist+e[i].val; 95 Q.push(LC); 96 } 97 } 98 return -1; 99 } 100 101 int hh() { 102 while(~scanf("%d%d",&n,&m)) { 103 init(); 104 for(int x,y,z;m--;) { 105 read(x);read(y);read(z); 106 add(x,y,z); 107 } 108 read(S);read(T);read(k); 109 spfa(); 110 int ans=Astar(); 111 printf("%d\n",ans); 112 } 113 return 0; 114 } 115 116 int sb=hh(); 117 int main() {;}
POJ 2449 Remmarguts' Date -K短路
标签:man sea pst std etc connect really required ice
原文地址:http://www.cnblogs.com/whistle13326/p/7413423.html