HDU 6582 Path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 244 Accepted Submission(s): 50
Problem Description
After doing some research on the neighbourhood, Tom found that the neighbourhood consists of exactly n houses, and some of them are connected with directed road. To visit his girlfriend, Jerry needs to start from his house indexed 1 and go along the shortest path to hers, indexed n.
Now Tom wants to block some of the roads so that Jerry has to walk longer to reach his girl's home, and he found that the cost of blocking a road equals to its length. Now he wants to know the minimum total cost to make Jerry walk longer.
Note, if Jerry can't reach his girl's house in the very beginning, the answer is obviously zero. And you don't need to guarantee that there still exists a way from Jerry's house to his girl's after blocking some edges.
Input
Each test case starts with a line containing two numbers n,m(1≤n,m≤10000), the number of houses and the number of one-way roads in the neighbourhood.
m lines follow, each of which consists of three integers x,y,c(1≤x,y≤n,1≤c≤109), denoting that there exists a one-way road from the house indexed x to y of length c.
Output
Sample Input
Sample Output
Source
Recommend
吐槽
这么过分,一定要发朋友圈博客。杭电多校第一场(见上面那个source),AC 1 题收场,就是这题。下两场可以休息了。本来第四题二分(https://www.cnblogs.com/wawcac-blog/p/11229277.html)也不难,但自己就是想不到。感觉自己现在还只会做板题。别人觉得难度更低的题,我就是想不出来。CF还是要接着打啊……
题意
原题在这https://www.cnblogs.com/wawcac-blog/p/7012556.html,上学路线的第二问,思路也在那了。只是数据范围增大了20倍,于是把Floyd改成dijkstra,几个int改成long long。理论上dinic是要T的,但它就是AC了……
源代码
#include <queue>
#include <stdio.h>
#include <string.h>
#include <algorithm> int T;
int n, m; struct Edge
{
int nxt, to;
long long w;
} e[], f[];
int cnt = , head[], fcnt = , fhead[]; //正向图与反向图
void add(int u, int v, long long w)
{
e[cnt] = {head[u], v, w};
head[u] = cnt++;
f[fcnt] = {fhead[v], u, w};
fhead[v] = fcnt++;
} long long dis[], fdis[];
bool vis[];
struct DijkHeap
{
int u;
long long d;
bool operator<(const DijkHeap &a) const
{
return d > a.d;
}
} dijktemp;
void dijkstra()
{
std::priority_queue<DijkHeap> q;
memset(dis, 0x7f, sizeof(long long) * (n + ));
memset(vis, , sizeof(bool) * (n + ));
dis[] = ;
q.push({, });
while (!q.empty())
{
dijktemp = q.top();
q.pop();
int u = dijktemp.u;
long long d = dijktemp.d;
vis[u] = ;
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (vis[v])
continue;
if (dis[v] > e[i].w + d)
{
dis[v] = e[i].w + d;
q.push({v, dis[v]});
}
}
} memset(fdis, 0x7f, sizeof(long long) * (n + ));
memset(vis, , sizeof(bool) * (n + ));
fdis[n] = ;
q.push({n, });
while (!q.empty())
{
dijktemp = q.top();
q.pop();
int u = dijktemp.u;
long long d = dijktemp.d;
vis[u] = ;
for (int i = fhead[u]; i; i = f[i].nxt)
{
int v = f[i].to;
if (vis[v])
continue;
if (fdis[v] > f[i].w + d)
{
fdis[v] = f[i].w + d;
q.push({v, fdis[v]});
}
}
}
} struct WEdge//最短路图
{
int nxt, to;
long long flow;
} we[];
int whead[] = {}, wcnt = ;
void wadd(int u, int v, long long f)
{
we[wcnt] = {whead[u], v, f};
whead[u] = wcnt++;
we[wcnt] = {whead[v], u, };
whead[v] = wcnt++;
} int dep[] = {};
bool bfs()
{
memset(dep, , sizeof(int) * (n + ));
std::queue<int> q;
dep[] = ;
q.push();
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = whead[u]; i; i = we[i].nxt)
{
long long v = we[i].to;
if (!dep[v] && we[i].flow)
{
dep[v] = dep[u] + ;
q.push(v);
}
}
}
return dep[n] != ;
} long long dfs(int u, long long fflow)
{
if (u == n || fflow == 0LL)
return fflow;
long long sum = ;
for (int i = whead[u]; i; i = we[i].nxt)
{
int v = we[i].to;
if (dep[v] == dep[u] + && we[i].flow)
{
long long delta = dfs(v, std::min(fflow - sum, we[i].flow));
sum += delta;
we[i].flow -= delta;
we[i ^ ].flow += delta;
if (fflow <= sum)
break;
}
}
if (!sum)
dep[u] = -;
return sum;
} long long dinic()
{
long long ans = ;
while (bfs())
{
while (long long temp = dfs(, 0x7f7f7f7f7f7f7f7f))
ans += temp;
}
return ans;
} void init()
{
cnt = fcnt = ;
wcnt = ;
memset(head, , sizeof(int) * (n + ));
memset(fhead, , sizeof(int) * (n + ));
memset(whead, , sizeof(int) * (n + ));
} int main()
{
//freopen("test.in","r",stdin);
scanf("%d", &T);
while (T--)
{
init();
scanf("%d%d", &n, &m);
for (int i = , u, v, w; i <= m; i++)
{
scanf("%d%d%d", &u, &v, &w);
add(u, v, (long long)w);
}
dijkstra();
for (int u = ; u <= n; u++)
{
for (int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to;
if (dis[u] + e[i].w + fdis[v] == dis[n])
{
wadd(u, v, e[i].w);
//printf("***%d %d %lld\n", u, v, e[i].w);
}
}
}
printf("%lld\n", dinic());
}
return ;
}
HDU 6582 Path的更多相关文章
- HDU - 6582 Path (最短路+最小割)
题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...
- 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】
一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...
- [最短路,最大流最小割定理] 2019 Multi-University Training Contest 1 Path
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6582 Path Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路
Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU 5492(DP) Find a path
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- HDU - 2290 Find the Path(最短路)
HDU - 2290 Find the Path Time Limit: 5000MS Memory Limit: 64768KB 64bit IO Format: %I64d & % ...
随机推荐
- 20191127 Spring Boot官方文档学习(4.13)
4.13.Messaging Spring框架为与消息传递系统集成提供了广泛的支持,从使用JmsTemplate简化JMS API到完整的异步接收消息的基础结构.Spring AMQP为高级消息队列协 ...
- netcat命令用法
1,端口扫描nc -z -v -n 172.31.100.7 21-25 2,聊天Server:nc -l 1567Client:nc 172.31.100.7 1567 3,文件传输Server:n ...
- php php-fpm、nginx和js
1 php-fpm是什么 php-fpm是php fastCGI process manager的缩写.它是php的进程管理器,对每个请求的处理都是一个进程. php-fpm管理了一个进程池,假如进程 ...
- 一、JVM — Java内存区域
Java 内存区域详解 写在前面 (常见面试题) 基本问题 拓展问题 一 概述 二 运行时数据区域 2.1 程序计数器 2.2 Java 虚拟机栈 2.3 本地方法栈 2.4 堆 2.5 方法区 2. ...
- AVCaptureSession拍照,摄像,载图总结
AVCaptureSession [IOS开发]拍照,摄像,载图总结 1 建立Session 2 添加 input 3 添加output 4 开始捕捉 5 为用户显示当前录制状态 6 捕捉 7 ...
- 给UITextView添加链接
给UITextView增加了链接 现在在iOS添加你自己的Twitter账户更加简单了,现在你可以给一个NSAttributedString增加链接了,然后当它被点击的时候唤起一个定制的action. ...
- nginx 启动关闭
#检查语法 /application/nginx/sbin/nginx –t #平滑重启 /application/nginx/sbin/nginx –s reload #不间断服务重启,将 pid ...
- losetup - 设 定 与 控 制 环回设备
总览 SYNOPSIS losetup [ -e encryption ] [ -o offset ] loop_device file losetup [ -d ] loop_device 描述 l ...
- python中字典的美化输出
一.背景 如果一个字典长度很大,直接print输出则比较难看,我们需要美化输出,可以借鉴json import json beautiful_format = json.dumps(your_dict ...
- Red Hat Enterprise Linux 8.0 安装
Red Hat Enterprise Linux 8.0 安装 本次安装通过使用VMware Workstation 15 pro 进行. 1.新建虚拟机 2.点击首页的创建新的虚拟机,或者点击标签栏 ...