216. 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]: 32

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;

Code

#include<algorithm>
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<queue> using namespace std; int n,m,s,t,Cnt,Next[5000001],head[5000001],to[5000001]; bool visit[5000001]; int dis[5000001],Cost[5000001],val[5000001],cost[5000001]; struct cmp{ bool operator()(int a,int b){ return dis[a]>dis[b]; } }; void Push(int x,int y,int z,int c){ ++Cnt; Next[Cnt]=head[x]; head[x]=Cnt; to[Cnt]=y; val[Cnt]=z; cost[Cnt]=c; } void dijkstra(){ priority_queue<int,vector<int>,cmp> Q; for(int i=1;i<=n;i++){ visit[i]=false; dis[i]=1000000001; Cost[i]=1000000001; } Q.push(s); visit[s]=true; dis[s]=0; Cost[s]=0; while(!Q.empty()){ int u=Q.top(); Q.pop(); visit[u]=false; for(int i=head[u];i;i=Next[i]){ int v=to[i]; if(dis[v]>dis[u]+val[i]||(Cost[v]>Cost[u]+cost[i]&&dis[v]==dis[u]+val[i])){ Cost[v]=Cost[u]+cost[i]; dis[v]=dis[u]+val[i]; if(!visit[v]){ visit[v]=true; Q.push(v); } } } } return; } int main(){ int u,v,w,c; while(scanf("%d%d",&n,&m)){ memset(head,0,sizeof(head)); if(n==0||m==0){ break; } for(int i=1;i<=m;i++){ scanf("%d%d%d%d",&u,&v,&w,&c); Push(u,v,w,c); Push(v,u,w,c); } scanf("%d%d",&s,&t); dijkstra(); printf("%d %d\n",dis[t],Cost[t]); } return 0; } 

This annoying problem.

It took a long time.

Guess you like

Origin www.cnblogs.com/xiongchongwen/p/11137597.html