ZOJ 3946-Highway Project【最短路的应用】(2016浙江省大学生程序设计竞赛)
2016-04-27 19:29
597 查看
Highway Project
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 areM highways can be built. Building thei-th highway costs
Ci dollars. It takes Di minutes to travel between cityXi andYi 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.
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).
[/code]
Time Limit: 2 Seconds
Memory Limit: 65536 KB
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 areM highways can be built. Building thei-th highway costs
Ci dollars. It takes Di minutes to travel between cityXi andYi 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 integerT 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
2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 1 1 2 3 1 2 4 5 0 3 1 1 0 1 1 1 0 2 10 10 2 1 2 1 2 3 1 2
Sample Output
4 3 4 4 解题思路: 给出一张图,边无向有两个,两个权分别为路的消耗的时间和花费的经前,点编号0-n-1,现在要你求从0建立到其他的路径,使各点花费总时间最少优先的情况下花费的金钱也最少。就是要考虑都是最短的情况下,谁的花费最少。 [code]#include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const ll inf=0x3f3f3f3f; const ll maxn=100005; ll edgenum,vis[maxn],head[maxn],dist[maxn],cost[maxn]; struct node { ll from,to,len,val; ll next; }edge[maxn*5]; void init() { edgenum=0; memset(head,-1,sizeof(head)); memset(dist,inf,sizeof(dist)); memset(vis,0,sizeof(vis)); memset(cost,inf,sizeof(cost)); } void add(ll u,ll v,ll d,ll c) { node tp={u,v,d,c,head[u]}; edge[edgenum]=tp; head[u]=edgenum++; } void dijkstra(ll n,ll st) { queue<ll> q; q.push(st); vis[st]=1; dist[st]=0; while(!q.empty()) { ll v=q.front();q.pop(); vis[v]=0; for(ll i=head[v];i!=-1;i=edge[i].next) { ll u=edge[i].to; if(dist[edge[i].to]==dist[v]+edge[i].len&&edge[i].val<cost[edge[i].to]) { cost[edge[i].to]=edge[i].val; } if(dist[edge[i].to]>dist[v]+edge[i].len) { dist[edge[i].to]=dist[v]+edge[i].len; cost[edge[i].to]=edge[i].val; if(!vis[edge[i].to]) { vis[edge[i].to]=1; q.push(edge[i].to); } } } } } int main() { //freopen("shuju.txt","r",stdin); ll t; scanf("%lld",&t); while(t--) { ll n,m; scanf("%lld%lld",&n,&m); init(); for(ll i=0;i<m;++i) { ll u,v,d,c; scanf("%lld%lld%lld%lld",&u,&v,&d,&c); add(u,v,d,c);add(v,u,d,c); } ll minlenth=0,mincost=0; dijkstra(n,0); for(ll i=1;i<n;++i) { minlenth+=dist[i]; mincost+=cost[i]; } printf("%lld %lld\n",minlenth,mincost); } return 0; }
[/code]
相关文章推荐
- Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcomm流程
- requestWindowFeature易错处
- redis安全与管理
- c++的bind()函数
- 【Tracking】面部表情实时传递系统
- CSS3属性transform详解之(旋转:rotate,缩放:scale,倾斜:skew,移动:translate)
- LRU 与 LFU 算法
- 【在Struts2中加入ajax异步发送请求给服务器】
- Android 自定义本地图片加载库,仿微信相册
- 详细讲解PHP的日期时间函数date()
- 关于函数指针和指针函数的分析
- Android bluetooth介绍(一):基本概念及硬件接口
- [置顶] Unity与Android交互-android的安装与配置
- UC网盘被关停 将与阿里淘盘合并?(案例说明云盘的成本才是重点,技术不是问题;与巨头竞争是重点)
- C++之时间统计
- 解读(三):分析MainActivity, 程序的界面主体框架
- Spring Learning Diary
- 常用的shell命令
- Css外边距合并
- android meta-data