The minimum cost Shortest

[Title Description:

You n points, m article undirected edges, each edge has a length d and expense p, s you starting end t, requires the shortest distance from the starting point to the end of its cost, the shortest distance if the plurality of routes, the output of the least expensive.

[Input Description:

A plurality of sets of data: each data is described as follows:

Input n, m, the number of points is 1 ~ n, then the m rows, each row number 4 a, B, d, p, indicates there is an edge between a and to b, and a length of d, is spent p . The last line number is two s, T; origin s, end point t.

m is 0 and n input end.

[Output] Description:

Output a line with two numbers, the shortest distance and cost.

[Sample input]:

3 2
1 2 5 6
2 3 4 5
1 3
0 0

[] Sample output:

9 11

[Time limit, and the range of data Description:

Time: 1s space: 128M

For 30% of the data: 1 <n <= 100

To 100% of the data: 1 <n <= 1000; 0 <m <100000; s = t; 1 <= d, p <= 1000!

The number of data sets <= 5, note cards often;

 

analysis:

This question is obviously one of the most short-circuit, but note that under the premise of making the most of a short circuit greedy, the value of minimum cost.

 

CODE:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <queue>
 7 using namespace std;
 8 const int M=1000005;
 9 const int oo=1<<30;
10 int n,m,s,t;
11 int next[M],head[M],to[M],adj[M],val[M];
12 int dist[M],ans[M];
13 struct node{
14     int id,d,v;
15     bool operator> (const node b) const {return d>b.d;}
16 }a[M];
17 priority_queue <node,vector<node>,greater<node> > Q;
18 int tot;
19 inline int get(){
20     char c=getchar();
21     int res=0;
22     while (c<'0'||c>'9') c=getchar();
23     while (c>='0'&&c<='9'){
24         res=(res<<3)+(res<<1)+c-'0';
25         c=getchar();
26     }
27     return res;
28 }
29 void add(int u,int v,int w,int p){
30     next[++tot]=head[u];
31     head[u]=tot;
32     to[tot]=v;
33     adj[tot]=w;
34     val[tot]=p;
35     return;
36 }
37 void dijkstra(){
38     for (int i=1;i<=n;i++) {dist[i]=oo;ans[i]=oo;}
39     dist[s]=0;
40     ans[s]=0;
41     Q.push((node){s,0,0});
42     while (!Q.empty()){
43         node x=Q.top(); 
44         Q.pop();
45         for (int i=head[x.id];i;i=next[i])
46             if (x.d+adj[i]<dist[to[i]]||(x.d+adj[i]==dist[to[i]]&&ans[to[i]]>x.v+val[i])){
47                 dist[to[i]]=x.d+adj[i];
48                 ans[to[i]]=x.v+val[i];
49                 Q.push((node){to[i],dist[to[i]],ans[to[i]]});
50             }
51     }
52     return;
53 }
54 int main(){
55     while (1){
56         n=get(),m=get();
57         if (n==0&&m==0) break;
58         memset(head,0,sizeof(head));
59         for (int i=1;i<=m;i++){
60             int u,v,w,p;
61             u=get(),v=get(),w=get(),p=get();
62             add(u,v,w,p);
63             add(v,u,w,p);
64         }
65         s=get(),t=get();
66         while (!Q.empty()) Q.pop();
67         memset(dist,0,sizeof(dist));
68         memset(ans,0,sizeof(ans));
69         dijkstra();
70         cout<<dist[t]<<" "<<ans[t]<<endl;
71     }
72     return 0;
73 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11119963.html