大概题意:

题意:N个点,M条带权有向边,求将K条边权值变为0的情况下,从点1到点N的最短路。

拓展:可以改变K条边的权值为x

做法:把每个点拆成k个点,分别表示还能使用多少次机会,构造新图。

实际写的时候,不用真的拆点,用dist[i][j]表示从源点出发到点i,免费j条边的最小花费,在dijkstra中维护分层即可,每个节点要存价值,编号,已经用的免费条数。

 #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; const int maxm = ; //±ß
const int maxn = ; //µã
const int inf = 0x3f3f3f3f; struct Edge
{
int to,v,next;
} edge[maxm];
struct node
{
long long val;
int num, h;
node(long long _val=, int _num=, int _d=):val(_val), num(_num),h(_d) {}
bool operator <(const node &tmp) const
{
return val > tmp.val;
}
};
int head[maxn];
int top;
int N, M, K;
long long dis[maxn][];
bool vis[maxn][];
long long ans = inf;
void init()
{
memset(head, -, sizeof(head));
top = ;
for(int i=; i<=N; i++)
{
for(int j=; j<=K; j++)
{
dis[i][j] = inf;
vis[i][j] = false;
}
}
ans = inf;
} void addedge(int from, int to, int v)
{
edge[top].to = to;
edge[top].v = v;
edge[top].next = head[from];
head[from] = top++;
}
void dijkstra()
{
priority_queue<node> que;
dis[][] = ;
que.push(node(, , ));
while(!que.empty())
{
node p = que.top();
que.pop();
int nown = p.num;
int h = p.h;
if(vis[nown][h])
continue;
vis[nown][h] = true;
for(int i=head[nown]; i!=-; i=edge[i].next)
{
Edge e = edge[i];
if(dis[e.to][h] > dis[nown][h] + e.v)
{
dis[e.to][h] = dis[nown][h] + e.v;
que.push(node(dis[e.to][h], e.to, h));
}
//修改的地方
if(dis[e.to][h+] > dis[nown][h] && h < K)
{
dis[e.to][h+] = dis[nown][h];
que.push(node(dis[nown][h], e.to, h+));
}
}
}
for(int i=; i<=K; i++)
{
ans = min(ans, dis[N][i]);
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d", &N, &M, &K);
init();
int u, v, c;
for(int i=; i<M; i++)
{
scanf("%d%d%d", &u, &v, &c);
addedge(u, v, c);
}
dijkstra();
printf("%lld\n", ans);
}
return ;
}

最新文章

  1. Hibernate(二)__简单实例入门
  2. Angular 单元格合并
  3. 自然语言22_Wordnet with NLTK
  4. [BTS] Exception occurred when persisting state to the database
  5. 在线白板,基于socket.io的多人在线协作工具
  6. 读书笔记2:HTTP协议
  7. WPF编译时提示“...不包含适合于入口点的静态‘Main’方法 ...”
  8. UI设计师不可不知的安卓屏幕知识
  9. 用ASP编写购物车代码
  10. Ajax comet XMLHttpRequest 异步
  11. 查询本地电脑IP地址
  12. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree
  13. STS 安装SVN插件
  14. Linux(CentOS)安装Node.JS和npm的两种方式(yum安装和源码安装)
  15. sixsix团队M2阶段Postmortem
  16. 对String值不可变的理解以及String类型的引用传递问题
  17. logger.debug的用处
  18. Ubuntu下快速部署安装 Nginx + PHP + MySQL 笔记
  19. SQL学习笔记四(补充-2-1)之MySQL SQL查询作业答案
  20. java随机数的生成

热门文章

  1. PHP5之前的构造函数与PHP5之后的构造函数的区别
  2. 蓝桥杯 算法训练 ALGO-129 特殊的数字四十
  3. List&lt;T&gt; JIT 分配策略
  4. C Primer Plus学习笔记(五)- C控制语句:循环
  5. Datapump tips
  6. 记录一次手机联系人整理(XML文件格式处理)
  7. [Python Study Notes]七彩散点图绘制
  8. [P3812][模板]线性基
  9. Windows版本Apache+php的Xhprof应用__[2]
  10. Python_pip_02_利用pip安装模块(以安装pyperclip为例)