Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 244    Accepted Submission(s): 50

Problem Description

Years later, Jerry fell in love with a girl, and he often walks for a long time to pay visits to her. But, because he spends too much time with his girlfriend, Tom feels neglected and wants to prevent him from visiting her.
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

The input begins with a line containing one integer T(1≤T≤10), the number of test cases.
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

Print T lines, each line containing a integer, the answer.

Sample Input

1
3 4
1 2 1
2 3 1
1 3 2
1 3 3
 

Sample Output

3
 

Source

 

Recommend

We have carefully selected several similar problems for you:  6590 6589 6588 6587 6586 

吐槽

  这么过分,一定要发朋友圈博客。杭电多校第一场(见上面那个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的更多相关文章

  1. HDU - 6582 Path (最短路+最小割)

    题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...

  2. 2019HDU多校第一场 6582 Path 【最短路+最大流最小割】

    一.题目 Path 二.分析 首先肯定要求最短路,然后如何确定所有的最短路其实有多种方法. 1 根据最短路,那么最短路上的边肯定是可以满足$dist[from] + e.cost = dist[to] ...

  3. [最短路,最大流最小割定理] 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 ...

  4. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  5. hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路

    Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...

  6. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  7. HDU 5492(DP) Find a path

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...

  8. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

  9. HDU - 2290 Find the Path(最短路)

    HDU - 2290 Find the Path Time Limit: 5000MS   Memory Limit: 64768KB   64bit IO Format: %I64d & % ...

随机推荐

  1. python 封装dlib模型进行人脸识别系统的登录认证

    1.直接上干货 #!/usr/bin/python # -*- coding: utf-8 -*- import time import dlib import numpy as np class f ...

  2. Javascript实现的图片隐写术

    javascript图片隐写术,感觉可以用它来干点有想法的事情   1.什么是图片隐写术? 权威的wiki说法是“隐写术是一门关于信息隐藏的技巧与科学,所谓信息隐藏指的是不让除预期的接收者之外的任何人 ...

  3. python 并发编程 多进程 模拟抢票

    抢票是并发执行 多个进程可以访问同一个文件 多个进程共享同一文件,我们可以把文件当数据库,用多个进程模拟多个人执行抢票任务 db.txt {"count": 1} 并发运行,效率高 ...

  4. 关于MySQL的安装使用心得

    MySQL浅浅地学习了几天,当然还是转到正轨Java上来了,昨天打了一串代码,测试注解来着,结果MySQL挂了~~~ 如何干净卸载MySQL帖子有很多,不再赘述,注册表是个好东西~~ 卸载了Mysql ...

  5. [DS+Algo] 009 树的介绍

    目录 1. 树的概念 2. 树的术语 3. 树的种类 4. 常见应用场景 5. 二叉树 1. 树的概念 每个结点(节点)有 0 个或多个子结点 没有父结点的结点称为根结点 每一个非根结点有且只有一个父 ...

  6. Luogu P5444 [APIO2019]奇怪装置

    题目 这种题目看上去就是有循环节的对吧. 在考场上,一个可行的方式是打表. 现在我们手推一下这个循环节. 记函数\(f(t)=(((t+\lfloor\frac tB\rfloor)\%A),(t\% ...

  7. Java中的sort

    Java中对集合排序有两种方式 Comparable和Comparator public static <T> void sort(List<T> list); 将集合中的数据 ...

  8. 分布式理论: CAP、BASE (转)

    分布式系统的CAP理论是由Eric Brewer于1999年首先提出的,又被称作布鲁尔定理(Brewer's theorem),CAP是对Consistency(一致性).Availability(可 ...

  9. Node.js+koa2

    const Koa = require('koa') const app = new Koa() const bodyParser = require('koa-bodyparser') app.us ...

  10. KMP解决最小循环节问题

    # 10035. 「一本通 2.1 练习 1」Power Strings [题目描述] 给定若干个长度 $\le 10^6$​​ 的字符串,询问每个字符串最多是由多少个相同的子字符串重复连接而成的.如 ...