解题关键:最短路的变形。

1、按顶点存储,$O(n^2)$

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define max_v 502
using namespace std;
typedef long long ll;
int pre[max_v],pathnum[max_v],toval[max_v],val[max_v];
int cost[max_v][max_v],d[max_v],used[max_v],V;
void dijkstra(int s){ //边权和点权都是通过顶点体现
fill(d,d+V,inf);
fill(used,used+V,);
fill(pre,pre+V,-); d[s]=;
pathnum[s]=;
toval[s]=val[s]; while(true){
int v=-;
for(int u=;u<V;u++){
if(!used[u]&&(v==-||d[u]<d[v])) v=u;
} if(v==-) break;
used[v]=true; for(int u=;u<V;u++){
if(used[u]==false&&cost[u][v]!=inf){//????
if(d[u]>d[v]+cost[v][u]){
d[u]=d[v]+cost[v][u];
pre[u]=v;
pathnum[u]=pathnum[v];
toval[u]=toval[v]+val[u];
}else if(d[u]==d[v]+cost[v][u]){
pathnum[u]+=pathnum[v];
if(toval[u]<toval[v]+val[u]){
pre[u]=v;
toval[u]=toval[v]+val[u];
}
}
}
}
}
}
vector<int> get_path(int t){
vector<int>path;
for(;t!=-;t=pre[t]) path.push_back(t);
reverse(path.begin(), path.end());
return path;
} int N,M,S,D;
int main(){
cin>>N>>M>>S>>D;
V=N;
for(int i=;i<N;i++) cin>>val[i];
for(int i=;i<V;i++){
for(int j=;j<V;j++){
if(i!=j) cost[i][j]=cost[i][j]=inf;
}
}
for(int i=;i<M;i++){
int a,b,c;
cin>>a>>b>>c;
cost[a][b]=cost[b][a]=c;
}
dijkstra(S);
printf("%d %d\n",pathnum[D],toval[D]);
vector<int>path=get_path(D);
for(int i=;i<path.size();i++){
printf("%d%c",path[i],i==path.size()-?'\n':' ');
}
return ;
}

2、按边存储 $O(nlogn)$

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#define max_v 502
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
struct edge{
int to,cost;
};
typedef pair<int,int>P;//按边存储,默认不可以自己到自己进行dp
int V,pre[max_v],pathnum[max_v],val[max_v],toval[max_v];
vector<edge>G[max_v];
int d[max_v];
void dijkstra(int s){
priority_queue<P,vector<P>,greater<P> >que;
fill(d,d+V,inf);
fill(pre,pre+V,-);
toval[s]=val[s];
pathnum[s]=;
d[s]=;
que.push(P(,s));
while(!que.empty()){
P p=que.top();que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++){
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
pathnum[e.to]=pathnum[v];
toval[e.to]=toval[v]+val[e.to];
pre[e.to]=v;
que.push(P(d[e.to],e.to));
}else if(d[e.to]==d[v]+e.cost){
pathnum[e.to]+=pathnum[v];
if(toval[e.to]<toval[v]+val[e.to]){
toval[e.to]=toval[v]+val[e.to];
pre[e.to]=v;
}
}
}
}
} vector<int> get_path(int t){
vector<int>path;
for(;t!=-;t=pre[t]) path.push_back(t);
reverse(path.begin(), path.end());
return path;
} int N,M,S,D;
int main(){
cin>>N>>M>>S>>D;
V=N;
for(int i=;i<N;i++) cin>>val[i];
for(int i=;i<M;i++){
int a,b,c;
cin>>a>>b>>c;
G[a].push_back((edge){b,c});
G[b].push_back((edge){a,c});
}
dijkstra(S);
printf("%d %d\n",pathnum[D],toval[D]);
vector<int>path=get_path(D);
for(int i=;i<path.size();i++){
printf("%d%c",path[i],i==path.size()-?'\n':' ');
}
return ;
}

3、代码三

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define max_v 502
using namespace std;
typedef long long ll;
int pre[max_v],pathnum[max_v],toval[max_v],val[max_v];
int cost[max_v][max_v],d[max_v],used[max_v],V;
void dijkstra(int s){ //边权和点权都是通过顶点体现
fill(d,d+V,inf);
fill(used,used+V,);
fill(pre,pre+V,-); d[s]=;
pathnum[s]=;
toval[s]=val[s]; while(true){
int v=-;
for(int u=;u<V;u++){
if(!used[u]&&(v==-||d[u]<d[v])) v=u;
} if(v==-) break;
used[v]=true; for(int u=;u<V;u++){
if(d[u]>d[v]+cost[v][u]){
d[u]=d[v]+cost[v][u];
pre[u]=v;
pathnum[u]=pathnum[v];
toval[u]=toval[v]+val[u];
}else if(d[u]==d[v]+cost[v][u]){
pathnum[u]+=pathnum[v];
if(toval[u]<toval[v]+val[u]){
pre[u]=v;
toval[u]=toval[v]+val[u];
}
}
}
}
}
vector<int> get_path(int t){
vector<int>path;
for(;t!=-;t=pre[t]) path.push_back(t);
reverse(path.begin(), path.end());
return path;
} int N,M,S,D;
int main(){
cin>>N>>M>>S>>D;
V=N;
for(int i=;i<N;i++) cin>>val[i];
for(int i=;i<V;i++){
for(int j=;j<V;j++){
cost[i][j]=cost[i][j]=inf;
}
}
for(int i=;i<M;i++){
int a,b,c;
cin>>a>>b>>c;
cost[a][b]=cost[b][a]=c;
}
dijkstra(S);
printf("%d %d\n",pathnum[D],toval[D]);
vector<int>path=get_path(D);
for(int i=;i<path.size();i++){
printf("%d%c",path[i],i==path.size()-?'\n':' ');
}
return ;
}

[patl2-001]紧急救援的更多相关文章

  1. swift 001

    swift 001  = 赋值是没有返回值的 所以 int a=10; int b=20; if(a=b){ printf("这个是错误的"); } swift  中的模运算 是支 ...

  2. [SDK2.2]Windows Azure Virtual Network (4) 创建Web Server 001并添加至Virtual Network

    <Windows Azure Platform 系列文章目录> 在上一章内容中,笔者已经介绍了以下两个内容: 1.创建Virtual Network,并且设置了IP range 2.创建A ...

  3. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数001·3D函数

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数001·3D函数 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“proce ...

  4. Android 开发错误信息001

    Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessExceptio ...

  5. python解无忧公主的数学时间编程题001.py

    python解无忧公主的数学时间编程题001.py """ python解无忧公主的数学时间编程题001.py http://mp.weixin.qq.com/s?__b ...

  6. php大力力 [005节] php大力力简单计算器001

    2015-08-22 php大力力005. php大力力简单计算器001: 上网看视频,看了半天,敲击代码,如下: <html> <head> <title>简单计 ...

  7. php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28

    php大力力 [001节]2015-08-21.php在百度文库的几个基础教程新手上路日记 大力力php 大力同学 2015-08-21 15:28 话说,嗯嗯,就是我自己说,做事认真要用表格,学习技 ...

  8. Web前端学习笔记(001)

    ....编号    ........类别    ............条目  ................明细....................时间 一.Web前端学习笔记         ...

  9. 001 The Hello World In Csharp

    C#是面向对象编程语言,语法和JAVA非常相似.接下来让我们看一下C#的Hello world. //001.cs using System; public class Hello { public ...

  10. nexus-2.13.0-01.war

    https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.13.0-01.war

随机推荐

  1. hzau 1202 GCD(矩阵快速幂)

    1202: GCD Time Limit: 1 Sec  Memory Limit: 1280 MBSubmit: 201  Solved: 31[Submit][Status][Web Board] ...

  2. C++Builder XE5对于C++11的支持真蛋疼

    好不容易下载个XE5,安装,破解,准备测试一下C++11中的lambda,写了一个最简单的表达式: [](){}; 居然编译通不过. 查了帮助文档,才晓得它的编译器分为BCC32和BCC64, BCC ...

  3. deep learning (五)线性回归中L2范数的应用

    cost function 加一个正则项的原因是防止产生过拟合现象.正则项有L1,L2 等范数,我看过讲的最好的是这个博客上的:机器学习中的范数规则化之(一)L0.L1与L2范数.看完应该就答题明白了 ...

  4. Oracle 11g 客户端连接 oracle 10g 服务端,乱码问题

    从网上搜索资料基本确定:字符集错误 Pl/sql 连接到oracle 数据库   “select userenv('language') from dual” 找到服务端的对应的字符集,拷贝之: 到本 ...

  5. LeetCode OJ:Contains DuplicateII(是否包含重复II)

    Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...

  6. BEC listen and translation exercise 13

    The old lady sits on a mobile chair every morning. He got a large fortune when his father died, but ...

  7. Codeforces Round #276 (Div. 2)C. Bits(构造法)

    这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1 ...

  8. Codeforces 802 ABC. Heidi and Library

    题目大意 你需要保证第\(i\)天时有第\(a_i\)种书.你可以在任何一天买书,买第\(i\)种书的代价为\(c_i\). 你最多同时拥有\(k\)本书,如果此时再买书,则必须先扔掉已拥有的一本书. ...

  9. 2.Linux下安装Jenkins

    1.安装jenkins的前提是安装好jdk环境,自行安装jdk,若安装成功后,使用一下命令即可成功安装jenkins: wget -O /etc/yum.repos.d/jenkins.repo ht ...

  10. java代码写个进程条

    总结:运用JProgressBar类.还有线程相关 package com.v; import java.awt.image.*; import java.awt.Color; import java ...