Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)
题目链接
http://codeforces.com/gym/101630/attachments
题解
zyb学长的题。
先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x\)与\(0\)取\(\max\), 跑最短路之后加上\(x\times k\)更新答案。
注意从\(0\)开始枚举(就相当于裸跑最短路)。
代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cassert>
#include<algorithm>
#include<queue>
#include<cstring>
#define llong long long
using namespace std;const int N = 3000;
const int M = 6000;
struct Edge
{int v,nxt; llong w;
} e[M+3];
int fe[N+3];
struct AEdge
{int u,v; llong w;
} ae[M+3];
struct DijNode
{int u; llong dis;DijNode() {}DijNode(int _u,llong _dis) {u = _u,dis = _dis;}bool operator <(const DijNode &arg) const {return dis>arg.dis;}
};
priority_queue<DijNode> pq;
llong dis[N+3];
bool vis[N+3];
int n,en,p,s,t,m;void addedge(int u,int v,llong w)
{en++; e[en].v = v; e[en].w = w;e[en].nxt = fe[u]; fe[u] = en;
}void clear()
{for(int i=1; i<=m; i++) e[i].v = e[i].nxt = e[i].w = 0;for(int i=1; i<=n; i++) fe[i] = 0;en = 0;
}llong Dijkstra()
{memset(dis,42,sizeof(dis)); memset(vis,false,sizeof(vis));dis[s] = 0ll; pq.push(DijNode(s,0));while(!pq.empty()){DijNode tmp = pq.top(); pq.pop(); int u = tmp.u;if(tmp.dis!=dis[u]) continue;if(vis[u]==true) continue;vis[u] = true;for(int i=fe[u]; i; i=e[i].nxt){int v = e[i].v;if(dis[v]>dis[u]+e[i].w && vis[v]==false){dis[v] = dis[u]+e[i].w;pq.push(DijNode(v,dis[v]));}}}return dis[t];
}int main()
{scanf("%d%d%d",&n,&m,&p); s = 1; t = n;for(int i=1; i<=m; i++){scanf("%d%d%I64d",&ae[i].u,&ae[i].v,&ae[i].w);}llong ans = 10000000000000000ll;for(int i=0; i<=m; i++){for(int j=1; j<=m; j++) addedge(ae[j].u,ae[j].v,max(ae[j].w-ae[i].w,0ll)),addedge(ae[j].v,ae[j].u,max(ae[j].w-ae[i].w,0ll));llong cur = Dijkstra();ans = min(ans,cur+ae[i].w*p);clear();}printf("%I64d\n",ans);return 0;
}
Codeforces Gym 101630J Travelling from Petersburg to Moscow (最短路)相关推荐
- Codeforces Gym 101630J Journey from Petersburg to Moscow (最短路)
题目链接 http://codeforces.com/gym/101630/attachments 题解 zyb学长的题. 先枚举第\(k\)大的边权,设其边权为\(x\),然后把每条边边权减掉\(x ...
- Codeforces Gym 101173 CERC 16 D BZOJ 4790 Dancing Disks
Codeforces Gym 101173 CERC 16 D & BZOJ 4790 Dancing Disks 强烈安利这道构造题目,非常有意思. 这里用到的思想是归并排序! 多路归并排序 ...
- Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)
Codeforces Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven) 题目来源: Codeforces 题意: 给出一些比赛, ...
- [Codeforces Gym 101651/100725B] Banal Tickets
Codeforces Gym 100725 题解: 先分两种情况, 积为000与积非0" role="presentation" style="position ...
- C. Tyler and Strings(组合数学,树状数组维护前缀和)(Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad i)
对我来说比较困难的一题了,尝试着自己写了一下,调不出来遂放弃. Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad in Info ...
- Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训
Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)简训 导语 涉及的知识点 题目 A Game B ...
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...
- Codeforces Gym 100676G Training Camp 状压dp
http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修 ...
最新文章
- python基础主要内容_python基础—python的介绍
- Lua基本语法-书写规范以及自带常用函数
- [云炬创业基础笔记]第十章企业的利润计划测试2
- Mysql5.7读写分离
- 转:OAUTH协议简介
- 【运维理论】RAID级别简介
- java 读取yaml配置文件
- “QQ尾巴病毒”核心技术的实现(转)
- DELL r340服务器U盘安装Centos7
- 怎么看计算机内存和独显,电脑独立显卡或集成显卡的显存大小怎么查看?
- DTI处理工具包PANDA小白记录
- 软件公司 sun公司 Oracle公司
- zz 用chrome模拟各种手机
- 替换运动学插件TRAC-IK
- 长城麒麟linux安装软件,在优麒麟20.04系统下安装软件建议到自带的软件商店中...
- win7系统没有诊断策略服务器,Win7提示诊断策略服务未运行无法诊断检查网络解决方法...
- C语言【海伦公式 求三角形面积】
- python整数和浮点数
- html引入本地css样式无效,vue在index.html里面引入css文件样式加载失败
- 凝思运行3D Unity程序