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. 【Linux开发】arm-linux-gnueabihf-gcc下载

    原文地址:http://www.veryarm.com/arm-linux-gnueabihf-gcc veryarm是个不错的网站,里面介绍了很多相关的基础知识. arm-linux-gnueabi ...

  2. js 中 json.stringfy()将对象、数组转换成字符串

    json.stringfy()将对象.数组转换成字符串 var student = new Object(); student.name = "Lanny"; student.ag ...

  3. [转帖]linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令

    linux screen 命令详解,xshell关掉窗口或者断开连接,查看断开前执行的命令 https://binwaer.com/post/12.html yun install -y screen ...

  4. 小解POJO、PO、BO、VO

    POJO :plain ordinary java object 无规则简单java对象 一个中间对象,可以转化为PO.DTO.VO. 一个简单的Java类,这个类没有实现/继承任何特殊的java接口 ...

  5. 数组转字符串 java8

    public static String arrayToString(int[] arr) { // 1,2,3... StringBuffer sb = new StringBuffer(); fo ...

  6. 搜索专题: HDU1016Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. Redux 关系图解

    Redux是一款状态管理库,并且提供了react-redux库来与React亲密配合, 但是总是傻傻分不清楚这2者提供的API和相应的关系.这篇文章就来理一理. Redux Redux 三大核心 Re ...

  8. react 前端项目技术选型、开发工具、周边生态

    react 前端项目技术选型.开发工具.周边生态 声明:这不是一篇介绍 React 基础知识的文章,需要熟悉 React 相关知识 主架构:react, react-router, redux, re ...

  9. mongodb启动报错,child process failed, exited with error number 1

    error: child process failed, exited with error number 1 第一次安装mongodb,随后启动一般不会出现上面的错误,出现这种错误的原因一般是mon ...

  10. LINQ 推迟查询的执行

    LINQ 在运行期间定义查询表达式时.查询就不会运行.查询会在迭代数据项是运行,例如: static void Main(string[] args) { List<string> lis ...