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. 【MM系列】SAP 各种冲销凭证

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 各种冲销凭证   前言部分 ...

  2. 【HBase】三、HBase和RDBMS的比较

      HBase作为一种NoSQL的数据库,和传统的类似于mysql这样的关系型数据库是有很大区别的,本文来对他们做一个对比分析,以便更加深入的了解HBase.   主要区别体现在以下六个方面:   1 ...

  3. 【Linux开发】将cmd中命令输出保存为TXT文本文件

    将cmd中命令输出保存为TXT文本文件 在网上看到一篇名为:"[转载]如何将cmd中命令输出保存为TXT文本文件" 例如:将Ping命令的加长包输出到D盘的ping.txt文本文件 ...

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

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

  5. exists、in和join比较

    这个根据实际情况具体分析 遇到问题了再具体分析就行.

  6. Windows Server 2012 上安装 dotNET Framework v3.5

    Windows Server 2012不能直接运行dotNET Framework v3.5安装程序进行安装,系统提供通过服务器管理器的添加功能和角色向导进行安装. 安装的前几个步骤再这里略去,在默认 ...

  7. Thinkphp3.2 Redis支持REDIS_AUTH验证

    原有的Redis类在Library/Think/Cache/Driver/中 换成下面的: <?php // +----------------------------------------- ...

  8. JavaScript的二维数组

    二维数组的初始化: 实例① var arr = [[1,2],['a','b']]; console.log(arr[1][0]); //a 第2列第1行所在的元素 实例② var arr = new ...

  9. Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑

    Python 入门之数据类型之间的相互转换 以及 在编程中会遇到的数据类型的坑 1.数据类型总结: 可变,不可变,有序,无序 (1)可变的数据类型:list dict set (2)不可变的数据类型: ...

  10. 思维体操: HDU1287破译密码

    破译密码 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...