题意:求出某个点到其他点的最短路,并求出在最短路情况下的最小费用

分析:用dijkstra求出最短路并同时更新最小费用即可,注意的是在最短路长度相同时费用取最小即可

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
const int INF=<<;
struct edge{
int to,dis,cost;
};
typedef pair<int,int> P;
int n,m;
vector<edge> g[maxn];
int d[maxn];
int pay[maxn];
void dijkstra(int s)
{
priority_queue<P,vector<P>, greater<P> >que;
fill(d,d+n+,INF);
fill(pay,pay+n+,INF);
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];
//cout<<e.to<<"abd"<<endl;
if(d[e.to]>d[v]+e.dis){
d[e.to]=d[v]+e.dis;
pay[e.to]=e.cost;
//num[e.to]=min(num[e.to],pay[e.to]);
que.push(P(d[e.to],e.to));
}else if(d[e.to]==d[v]+e.dis){
pay[e.to]=min(e.cost,pay[e.to]);
}
}
}
}
int main()
{
while(cin>>n>>m)
{
if(n+m==) break;
for(int i=;i<=maxn-;i++) g[i].clear();
for(int i=;i<m;i++)
{
int u,v,d,c;
scanf("%d%d%d%d",&u,&v,&d,&c);
edge e;
e.to=v,e.dis=d,e.cost=c;
g[u].push_back(e);
edge e1;
e1.to=u,e1.dis=d,e1.cost=c;
g[v].push_back(e1);
}
dijkstra();
int sum=;
for(int i=;i<=n;i++){
sum+=pay[i];
}
cout<<sum<<endl;
}
return ;
}

AOJ2249最短路+最小费用的更多相关文章

  1. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  2. bzoj1927最小费用最大流

    其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→   =_=你TM逗我 刚要删突然感觉dinic的模 ...

  3. ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)

    将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...

  4. hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***

    题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙,          每个逮捕队伍在每个城市可以选 ...

  5. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  6. 最小费用最大流 POJ2195-Going Home

    网络流相关知识参考: http://www.cnblogs.com/luweiseu/archive/2012/07/14/2591573.html 出处:優YoU http://blog.csdn. ...

  7. 【进阶——最小费用最大流】hdu 1533 Going Home (费用流)Pacific Northwest 2004

    题意: 给一个n*m的矩阵,其中由k个人和k个房子,给每个人匹配一个不同的房子,要求所有人走过的曼哈顿距离之和最短. 输入: 多组输入数据. 每组输入数据第一行是两个整型n, m,表示矩阵的长和宽. ...

  8. POJ 2516 最小费用最大流

    每一种货物都是独立的,分成k次最小费用最大流即可! 1: /** 2: 因为e ==0 所以 pe[v] pe[v]^1 是两条相对应的边 3: E[pe[v]].c -= aug; E[pe[v]^ ...

  9. Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...

随机推荐

  1. bjective-C 中核心处理字符串的类是 NSString 与 NSMutableString

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  2. C#设置默认打印机

    项目中,需要选择打印机,切换打印机.demo如下(wpf应用程序): Xaml: <Window x:Class="PrintersApp.MainWindow" xmlns ...

  3. WPF Template模版之DataTemplate与ControlTemplate【一】

    WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -- ...

  4. Python 3.5.1 Syntax & APIs(Continue Updating..

    print(x, end=' ') instead of print(x) to escape the default line-changing-output. print(str.ljust(si ...

  5. BCDBOOT命令参数介绍

    BCDboot 命令行选项 更新时间: 2013年10月 应用到: Windows 8, Windows 8.1, Windows Server 2012, Windows Server 2012 R ...

  6. 正确使用#include和前置声明(forward declaration)

    http://blog.csdn.net/SpriteLW/article/details/965702

  7. HDU-1232--畅通工程(最小生成树)

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...

  8. CentOS 下mysql 的安装

    1.安装mysql服务器 yum -y install mysql-server 2.装入service启动服务 /etc/rc.d/init.d/mysqld start 3.设置mysql服务开机 ...

  9. CentOS-Desktop版service network restart报错

    出错情况: [root@localhost]# service network restart正在关闭接口 eth0: 设备状态:3 (断开连接)                            ...

  10. openstack镜像如何在vmware 环境中运行

    1.云镜像文件下载地址: http://sahara-files.mirantis.com/sahara-juno-spark-1.0.0-ubuntu-14.04.qcow2(安装有sahara-s ...