zoj5718
2016-04-24 10:38
127 查看
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 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th
highway costs Ci dollars. It takes Di minutes to travel between city Xi 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 city i (1
≤i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.
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).
题意:n个城市,0-n-1,0表示首都,在城市之间要修建一些高速公路(双向),建每条高速公路都有一些花费,以及通过该公路的时间,要求是
由0城市到其余n-1个城市总时间最短,然后花费最少,优先时间最短;
我的做法是dijstra+优先队列,按照总时间优先排序,次按照花费排序,同时更新到达i点的最小时间,最后将到达每一点的时间加和,的姐!
有一个坑,要用时间和花费和会爆long long
ac代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100500,maxm=400550,inf=0x3f3f3f3f;
struct Edge
{
int to,next,tim,cos ;
}edge[maxm*2];
struct Pair
{
LL T,V,C;
Pair(){;}
Pair(LL t,LL c,LL v)
{
T=t,V=v,C=c;
}
friend bool operator <(const Pair a,const Pair b)
{
if(a.T!=b.T)
return a.T>b.T;
else return a.C>b.C;
}
};
int tot,head[maxn];
LL cost[maxn];
bool vis[maxn];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
}
void add_edge(int u,int v,int t,int c)
{
edge[tot].to=v;
edge[tot].tim=t;
edge[tot].cos=c;
edge[tot].next=head[u];
head[u]=tot++;
}
LL dijstra(int start)
{
memset(vis,false,sizeof(vis));
memset(cost,inf,sizeof(cost));
cost[start]=0;
LL ans=0;
priority_queue<Pair>que;
que.push(Pair(0,0,start));
while(!que.empty())
{
Pair now=que.top();
que.pop();
if(vis[now.V])
continue;
vis[now.V]=true;
ans+=now.C;
for(int i=head[now.V];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(cost[to]>cost[now.V]+edge[i].tim)
cost[to]=cost[now.V]+edge[i].tim;
que.push(Pair( (LL)edge[i].tim+now.T,(LL)edge[i].cos,(LL)to));
}
}
return ans;
}
int main()
{
int u,v,t,c;
int n,m;
int TT;
cin>>TT;
while(TT--)
{
scanf("%d%d",&n,&m);
init();
if(n==0&&m==0)break;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&t,&c);
add_edge(u,v,t,c);
add_edge(v,u,t,c);
}
LL T=0,C=0;
C=dijstra(0);
for(int i=1;i<n;i++)
T+=cost[i];
printf("%lld %lld\n",T,C);
}
return 0;
}
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 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th
highway costs Ci dollars. It takes Di minutes to travel between city Xi 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 city i (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
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 34 4
题意:n个城市,0-n-1,0表示首都,在城市之间要修建一些高速公路(双向),建每条高速公路都有一些花费,以及通过该公路的时间,要求是
由0城市到其余n-1个城市总时间最短,然后花费最少,优先时间最短;
我的做法是dijstra+优先队列,按照总时间优先排序,次按照花费排序,同时更新到达i点的最小时间,最后将到达每一点的时间加和,的姐!
有一个坑,要用时间和花费和会爆long long
ac代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn=100500,maxm=400550,inf=0x3f3f3f3f;
struct Edge
{
int to,next,tim,cos ;
}edge[maxm*2];
struct Pair
{
LL T,V,C;
Pair(){;}
Pair(LL t,LL c,LL v)
{
T=t,V=v,C=c;
}
friend bool operator <(const Pair a,const Pair b)
{
if(a.T!=b.T)
return a.T>b.T;
else return a.C>b.C;
}
};
int tot,head[maxn];
LL cost[maxn];
bool vis[maxn];
void init()
{
tot=0;
memset(head,-1,sizeof(head));
}
void add_edge(int u,int v,int t,int c)
{
edge[tot].to=v;
edge[tot].tim=t;
edge[tot].cos=c;
edge[tot].next=head[u];
head[u]=tot++;
}
LL dijstra(int start)
{
memset(vis,false,sizeof(vis));
memset(cost,inf,sizeof(cost));
cost[start]=0;
LL ans=0;
priority_queue<Pair>que;
que.push(Pair(0,0,start));
while(!que.empty())
{
Pair now=que.top();
que.pop();
if(vis[now.V])
continue;
vis[now.V]=true;
ans+=now.C;
for(int i=head[now.V];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(cost[to]>cost[now.V]+edge[i].tim)
cost[to]=cost[now.V]+edge[i].tim;
que.push(Pair( (LL)edge[i].tim+now.T,(LL)edge[i].cos,(LL)to));
}
}
return ans;
}
int main()
{
int u,v,t,c;
int n,m;
int TT;
cin>>TT;
while(TT--)
{
scanf("%d%d",&n,&m);
init();
if(n==0&&m==0)break;
for(int i=0;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&t,&c);
add_edge(u,v,t,c);
add_edge(v,u,t,c);
}
LL T=0,C=0;
C=dijstra(0);
for(int i=1;i<n;i++)
T+=cost[i];
printf("%lld %lld\n",T,C);
}
return 0;
}
相关文章推荐
- JavaScript中的数据类型检测问题
- Ubuntu14.10 更新源,现在很多源已经停用了建议使用ubuntu15以上
- Xamarin.Android之布局文件智能提示问题
- 234. Palindrome Linked List
- 《CSS3实战》笔记--弹性盒模型(一)
- Linux内核学习总结
- [置顶] Rails环境搭建与部署(Nginx+Passenger)
- Android中调用系统的文件浏览器及自制简单的文件浏览器
- [C#] 逆袭——自制日刷千题的AC自动机攻克HDU OJ
- javascript 正则表达式语法
- 计算机视觉和人工智能领域:教程、网站、博客记录(陆续更新)
- 20145311 《Java程序设计》第八周学习总结
- 我的在线简历
- javascript中apply()和call()方法的区别
- CSS 图片替换文字方案
- BZOJ4290 传送门
- 几个可以装逼的英文和中文技术文档
- 20145315 《Java程序设计》第八周学习总结
- php通过curl post和get发送json数据实例 curl命令转为php源码
- bzoj 1821: [JSOI2010]Group 部落划分 Group