ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价。要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价。
题目分析:这是16年浙江省赛的一道题。先求出0到所有点的最短路,然后找出所有可能在最短路径上的边,最后在每一个节点的入边之中都选一条具有最小代价的边。多么简单的一道题!!!
代码如下:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<vector>
# include<queue>
# include<list>
# include<set>
# include<map>
# include<string>
# include<cmath>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define LL long long const int N=1005;
const int INF=1000000000;
const LL oo=1000000000000005; struct Edge
{
int to,nxt;
LL c,d;
}; int n,m,cnt;
Edge e[N*200];
LL d[N*100];
int head[N*100];
vector<int>pre[N*100]; void add(int u,int v,LL d,LL c)
{
e[cnt].to=v;
e[cnt].d=d;
e[cnt].c=c;
e[cnt].nxt=head[u];
head[u]=cnt++;
} void solve()
{
for(int i=0;i<n;++i)
pre[i].clear(); fill(d,d+n,oo);
d[0]=0;
queue<int>q;
q.push(0);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(d[v]>d[u]+e[i].d){
d[v]=d[u]+e[i].d;
q.push(v);
}
}
} q.push(0);
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].nxt){
int v=e[i].to;
if(d[v]==d[u]+e[i].d){
pre[v].push_back(i);
q.push(v);
}
}
} LL ans1=0,ans2=0;
for(int i=0;i<n;++i){
ans1+=d[i];
LL minn=oo;
for(int j=0;j<pre[i].size();++j)
minn=min(minn,e[pre[i][j]].c);
if(minn==oo) continue;
ans2+=minn;
}
printf("%lld %lld\n",ans1,ans2);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
cnt=0;
memset(head,-1,sizeof(head));
int a,b;
LL c,d;
for(int i=0;i<m;++i){
scanf("%d%d%lld%lld",&a,&b,&d,&c);
add(a,b,d,c);
add(b,a,d,c);
}
solve();
}
return 0;
}
ZOJ-3946 Highway Project (最短路)的更多相关文章
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...
随机推荐
- SharePoint表单和工作流 - Nintex篇(六)
博客地址 http://blog.csdn.net/foxdave 接上篇点击打开链接 在工作流Action的配置对话框中,点击"Edit with Nintex Forms",上 ...
- python文件打包格式,pip包管理
1..whl是python文件的一种打包格式, 在有些情况下,可以将文件的后缀名改为.zip并解压 2.cmd中,提示pip版本太低,先升级pip pip install --upgrade pi ...
- C#判断IP地址是否合法函数-使用正则表达式-2个 (转)
public bool IsCorrenctIP(string ip){ string pattrn=@"(/d{1,2}|1/d/d|2[0-4]/d|25[0-5])/.(/d{1,2} ...
- JS 中的五个假值
1."", undefined, null, 0, NaN 除了这五个假值以外,其他所有值转布尔类型都是true.还有一个特殊的false.
- Ubuntu 14.10 下安装中文输入法
系统默认带的是IBUS,这个不怎么好用,我们需要安装一个新的框架FCITX 1 打开软件中心,输入fcitx,安装flexible input method framework 2 下载需要的输入法, ...
- char, signed char, and unsigned char in C++
关于这三者的区别stackoverrflow里有一个答案是这样说的: 3.9.1 Fundamental types [basic.fundamental] 1 Objects declared as ...
- CentOS中vsftp安装与配置
http://blog.chinaunix.net/uid-7271021-id-3086186.html 553 Could not create file 解决办法 [root@localhost ...
- 未来WEB程序员
作为一名程序员,如果你想在这个领域内继续向前进步或者在当前的经济形势下保持不被炒鱿鱼,那么你就决不应当自满自足,你需要继续学习.近日,著名IT评论员Justin James在他的博客中列出了未来五年程 ...
- Linux----七个有效的文本编辑习惯
如果你要花大量的时间键入文本, 写程序或编写HTML脚本, 你可以通过有效地使用一个好的编辑器来替你节省时间. 本文将引导你如果快速地完成你的编辑工作, 并且减少你的错误. 本文将以开放源码软件Vim ...
- Git ~ 添加远程仓库 ~Git
现在的情景是 , 你已经在本地创建了一个Git仓库后 , 又想在 Github 创建一个Git 仓库并且让这两个仓库进行远程同步 , 这样Github 上的仓库既可以作为备份 ,有可以让其他人通过仓库 ...