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. 【SQL系列】从SQL语言的分类谈COMMIT和ROLLBACK的用法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[SQL系列]从SQL语言的分类谈COMMIT和 ...

  2. 浅谈html5在vr中的应用

    使用过HTML5制作动画过程的开发者都知道,HTML5页面给人一种逼真的感觉,同时HTML也是可以制作VR页面,但是需要你熟练HTML5与JavaScript开发过程,所以在有必要的情况下,我们可以用 ...

  3. 程序员听到bug后的N种反应…

    程序员的世界里, 不止有代码, 还有bug,bug,bug- 当出现bug时, 程序员们的反应是怎样的呢? 作者:苏小喵,来源:小花小画(微信号:hua-little) - END - 推荐阅读: 1 ...

  4. 使用foreach一次性添加多个单选按钮

    <?php //这个函数创建一个单选按钮 //函数接收一个论据:值 //函数也会让按钮有"黏性"-->记住你是如何填写它的. function create_radio ...

  5. 洛谷 P1484 种树(优先队列,贪心,链表)

    传送门 解题思路 第一眼的贪心策略:每次都选最大的. 但是——不正确! 因为选了第i个树,第i-1和i-1棵树就不能选了.所以,要有一个反悔操作. 选了第i个后,我们就把a[i]的值更新为a[l[i] ...

  6. Codeforces 1262D Optimal Subsequences(BIT+二分)

    首先比较容易想到肯定是前k大的元素,那么我们可以先对其进行sort,如果数值一样返回下标小的(见题意),接下里处理的时候我们发现需要将一个元素下标插入到有序序列并且需要访问第几个元素是什么,那么我们可 ...

  7. Simpsons’ Hidden Talents

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...

  8. @Transactional实现原理

    Transactional是spring中定义的事务注解,在方法或类上加该注解开启事务.主要是通过反射获取bean的注解信息,利用AOP对编程式事务进行封装实现.AOP对事务的封装可以看我的这篇文章的 ...

  9. luogu P1399 [NOI2013]快餐店

    传送门 注意到答案为这个基环树直径\(/2\) 因为是基环树,所以考虑把环拎出来.如果直径不过环上的边,那么可以在环上每个点下挂的子树内\(dfs\)求得.然后如果过环上的边,那么环上的部分也是一条链 ...

  10. SpringMVC简单介绍及执行

    SpringMVC介绍 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一 ...