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

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(1T10), the number of test cases.
Each test case starts with a line containing two numbers n,m(1n,m10000), 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(1x,yn,1c109), 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了……

源代码

  1 #include <queue>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #include <algorithm>
  5 
  6 int T;
  7 int n, m;
  8 
  9 struct Edge
 10 {
 11     int nxt, to;
 12     long long w;
 13 } e[10010], f[10010];
 14 int cnt = 1, head[10010], fcnt = 1, fhead[10010]; //正向图与反向图
 15 void add(int u, int v, long long w)
 16 {
 17     e[cnt] = {head[u], v, w};
 18     head[u] = cnt++;
 19     f[fcnt] = {fhead[v], u, w};
 20     fhead[v] = fcnt++;
 21 }
 22 
 23 long long dis[10010], fdis[10010];
 24 bool vis[10010];
 25 struct DijkHeap
 26 {
 27     int u;
 28     long long d;
 29     bool operator<(const DijkHeap &a) const
 30     {
 31         return d > a.d;
 32     }
 33 } dijktemp;
 34 void dijkstra()
 35 {
 36     std::priority_queue<DijkHeap> q;
 37     memset(dis, 0x7f, sizeof(long long) * (n + 2));
 38     memset(vis, 0, sizeof(bool) * (n + 2));
 39     dis[1] = 0;
 40     q.push({1, 0});
 41     while (!q.empty())
 42     {
 43         dijktemp = q.top();
 44         q.pop();
 45         int u = dijktemp.u;
 46         long long d = dijktemp.d;
 47         vis[u] = 1;
 48         for (int i = head[u]; i; i = e[i].nxt)
 49         {
 50             int v = e[i].to;
 51             if (vis[v])
 52                 continue;
 53             if (dis[v] > e[i].w + d)
 54             {
 55                 dis[v] = e[i].w + d;
 56                 q.push({v, dis[v]});
 57             }
 58         }
 59     }
 60 
 61     memset(fdis, 0x7f, sizeof(long long) * (n + 2));
 62     memset(vis, 0, sizeof(bool) * (n + 2));
 63     fdis[n] = 0;
 64     q.push({n, 0});
 65     while (!q.empty())
 66     {
 67         dijktemp = q.top();
 68         q.pop();
 69         int u = dijktemp.u;
 70         long long d = dijktemp.d;
 71         vis[u] = 1;
 72         for (int i = fhead[u]; i; i = f[i].nxt)
 73         {
 74             int v = f[i].to;
 75             if (vis[v])
 76                 continue;
 77             if (fdis[v] > f[i].w + d)
 78             {
 79                 fdis[v] = f[i].w + d;
 80                 q.push({v, fdis[v]});
 81             }
 82         }
 83     }
 84 }
 85 
 86 struct WEdge//最短路图
 87 {
 88     int nxt, to;
 89     long long flow;
 90 } we[20010];
 91 int whead[10010] = {0}, wcnt = 2;
 92 void wadd(int u, int v, long long f)
 93 {
 94     we[wcnt] = {whead[u], v, f};
 95     whead[u] = wcnt++;
 96     we[wcnt] = {whead[v], u, 0};
 97     whead[v] = wcnt++;
 98 }
 99 
100 int dep[10010] = {0};
101 bool bfs()
102 {
103     memset(dep, 0, sizeof(int) * (n + 2));
104     std::queue<int> q;
105     dep[1] = 1;
106     q.push(1);
107     while (!q.empty())
108     {
109         int u = q.front();
110         q.pop();
111         for (int i = whead[u]; i; i = we[i].nxt)
112         {
113             long long v = we[i].to;
114             if (!dep[v] && we[i].flow)
115             {
116                 dep[v] = dep[u] + 1;
117                 q.push(v);
118             }
119         }
120     }
121     return dep[n] != 0;
122 }
123 
124 long long dfs(int u, long long fflow)
125 {
126     if (u == n || fflow == 0LL)
127         return fflow;
128     long long sum = 0;
129     for (int i = whead[u]; i; i = we[i].nxt)
130     {
131         int v = we[i].to;
132         if (dep[v] == dep[u] + 1 && we[i].flow)
133         {
134             long long delta = dfs(v, std::min(fflow - sum, we[i].flow));
135             sum += delta;
136             we[i].flow -= delta;
137             we[i ^ 1].flow += delta;
138             if (fflow <= sum)
139                 break;
140         }
141     }
142     if (!sum)
143         dep[u] = -1;
144     return sum;
145 }
146 
147 long long dinic()
148 {
149     long long ans = 0;
150     while (bfs())
151     {
152         while (long long temp = dfs(1, 0x7f7f7f7f7f7f7f7f))
153             ans += temp;
154     }
155     return ans;
156 }
157 
158 void init()
159 {
160     cnt = fcnt = 1;
161     wcnt = 2;
162     memset(head, 0, sizeof(int) * (n + 2));
163     memset(fhead, 0, sizeof(int) * (n + 2));
164     memset(whead, 0, sizeof(int) * (n + 2));
165 }
166 
167 int main()
168 {
169     //freopen("test.in","r",stdin);
170     scanf("%d", &T);
171     while (T--)
172     {
173         init();
174         scanf("%d%d", &n, &m);
175         for (int i = 1, u, v, w; i <= m; i++)
176         {
177             scanf("%d%d%d", &u, &v, &w);
178             add(u, v, (long long)w);
179         }
180         dijkstra();
181         for (int u = 1; u <= n; u++)
182         {
183             for (int i = head[u]; i; i = e[i].nxt)
184             {
185                 int v = e[i].to;
186                 if (dis[u] + e[i].w + fdis[v] == dis[n])
187                 {
188                     wadd(u, v, e[i].w);
189                     //printf("***%d   %d   %lld\n", u, v, e[i].w);
190                 }
191             }
192         }
193         printf("%lld\n", dinic());
194     }
195     return 0;
196 }

 

posted @ 2019-07-22 21:22  wawcac  阅读(844)  评论(0编辑  收藏  举报