zoj--3946--Highway Project(SPFA)
来源:互联网 发布:csgo淘宝买激活码 编辑:程序博客网 时间:2024/05/22 01:49
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.
The Marjar Empire has N cities (including the capital), indexed from 0 toN - 1 (the capital is 0) and there are M highways can be built. Building thei-th highway costs Ci dollars. It takes Di minutes to travel between cityXi and Yi on the i-th highway.
Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to cityi (1 ≤ i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first contains two integers N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi,Yi, Di, Ci (0 ≤Xi, Yi < N, 0 < Di,Ci < 105).
Output
For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.
Sample Input
24 50 3 1 10 1 1 10 2 10 102 1 1 12 3 1 24 50 3 1 10 1 1 10 2 10 102 1 2 12 3 1 2
Sample Output
4 34 4
题意:给出n个城市还有m条路,每条路都有一定的花费,建成之后跑起来也需要一定的时间,0是首都,现在要求0到每一个城市的距离都最短,输出花费,并且求出在最短距离下的花费有多少
思路:只是一个很简单的SPFA而已,因为点有点多,map存不下,所以地杰斯特拉肯定是不可以的,做这个题的时候没想到怎么存放花费,其实就是以时间为主要条件,然后花费作为第二个条件,dis[i]表示到达城市i的最短距离,cost[i]表示到达i所需要的最少花费的边,因为求dis的时候已经把树建好了,所以cost不需要考虑建树的问题,直接求最小就行
数据有点大,最好用long long
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;#define MAXN 200000+100#define INF 0x3f3f3f3f#define ll long long struct node{ll u,v,time,cost;ll next;}edge[MAXN*10];ll cnt,n,m,vis[MAXN],head[MAXN],dis[MAXN],cost[MAXN];void add(ll u,ll v,ll time,ll len){node E={u,v,time,len,head[u]};edge[cnt]=E;head[u]=cnt++;}void SPFA(){memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));memset(cost,INF,sizeof(cost));queue<ll>q;vis[0]=1;dis[0]=0;q.push(0);while(!q.empty()){ll u=q.front();q.pop();vis[u]=0;for(ll i=head[u];i!=-1;i=edge[i].next){node E=edge[i];if(dis[E.v]>dis[E.u]+E.time){dis[E.v]=dis[E.u]+E.time;cost[E.v]=E.cost;if(!vis[E.v]){vis[E.v]=1;q.push(E.v);}}if(dis[E.v]==dis[E.u]+E.time&&cost[E.v]>E.cost){cost[E.v]=E.cost;}}}}int main(){ll t;scanf("%lld",&t);while(t--){scanf("%lld%lld",&n,&m);cnt=0;memset(head,-1,sizeof(head));ll u,v,time,len;for(ll i=0;i<m;i++){scanf("%lld%lld%lld%lld",&u,&v,&time,&len);add(u,v,time,len);add(v,u,time,len);}SPFA();long long sdis=0,scost=0;for(ll i=1;i<n;i++){sdis+=dis[i];scost+=cost[i];}printf("%lld %lld\n",sdis,scost);}return 0;}
- zoj--3946--Highway Project(SPFA)
- ZOJ 3946Highway Project【spfa】
- ZOJ 3946 Highway Project (spfa)
- zoj 3946 Highway Project (spfa + 最小生成树)
- ZOJ--Highway Project(spfa+两级选择)
- ZOJ 3946 Highway Project(spfa最短路+记忆化搜索)
- ZOJ 3946 Highway Project SPFA 两个限制条件
- ZOJ 3946Highway Project
- zoj 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ 3946 Highway Project
- ZOJ-3946-Highway Project(最短路)
- ZOJ 3946 Highway Project(最短路)
- ZOJ 3946 Highway Project(Dijkstra)
- zoj 3946 Highway Project【SPFA多个性质的最优化】
- zoj 3946Highway Project(最短路改)
- ZOJ 3946 Highway Project 单源最短路
- ZOJ Problem Set - 3946 Highway Project
- 快速排序讲解(粗略)
- Mysql join连接
- 官网 Mysql Installing MySQL on Linux Using RPM Packages from Oracle
- EditText限制不能输入空格,汉字和字符不超过28
- 【BZOJ 4517】排列计数 - 排列组合+乘法逆元
- zoj--3946--Highway Project(SPFA)
- H.264(MPEG-4 AVC)级别(Level)、DPB 与 MaxDpbMbs 详解
- python编码问题
- [Lintcode] Unique Paths I,II
- 算法与数据结构(JAVA)-排序算法总结
- 【C++】输出格式
- [POJ1655]Balancing Act(树形dp)
- java 栈与队列
- hadoop完全分布式集群安装