HDU 6201 transaction transaction transaction(SPFA算法求最长路径)
来源:互联网 发布:mac的windows支持软件 编辑:程序博客网 时间:2024/06/05 17:04
Kelukin is a businessman. Every day, he travels around cities to do some business. On August 17th, in memory of a great man, citizens will read a book named "the Man Who Changed China". Of course, Kelukin wouldn't miss this chance to make money, but he doesn't have this book. So he has to choose two city to buy and sell.
As we know, the price of this book was different in each city. It isai yuan in i t city. Kelukin will take taxi, whose price is 1 yuan per km and this fare cannot be ignored.
There aren−1 roads connecting n cities. Kelukin can choose any city to start his travel. He want to know the maximum money he can get.
As we know, the price of this book was different in each city. It is
There are
For each test case:
first line contains an integer
second line contains
then follows
1 4 10 40 15 30 1 2 301 3 23 4 10
8
题解:
比赛的时候没写出这一题。。因为一直觉得是树形dp,没敢动,比完赛听别人说用图论方法也可以做出来orz,想了很久也没相出来,还是搜博客做出来的,弱哭
思路:
假设一个源点和汇点,然后将源点与其他点建一条值为v的路,将汇点和其他点建一条值为-v的路,其他点之间的边就都为负的权值的路。。然后以源点为起点,汇点为终点用SPFA跑一遍最长路径就好了。。。这个建图太巧妙了,我是想不出来,这样建图,源点和汇点的最长路就是答案。。orz
还有就是我用静态数组的邻接表来写这题就无限TLE,估计是一个点连接的边特别的多,用动态的邻接表800ms就过了。。
代码:
#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>#define ll long long#define INF 100861111using namespace std;struct edge{ int to; int v;};vector<edge>a[100005];int vis[100005];int dis[100005];int n;int ans;void addEdge(int from,int to,int v){ edge t; t.to=to; t.v=v; a[from].push_back(t);}void spfa(){ queue<int>q; int now,next; now=0; q.push(now); int i; while(!q.empty()) { now=q.front(); vis[now]=0; q.pop(); for(i=0;i<a[now].size();i++) { next=a[now][i].to; if(dis[now]+a[now][i].v>dis[next]) { dis[next]=dis[now]+a[now][i].v; if(!vis[next]) { vis[next]=1; q.push(next); } } } } printf("%d\n",dis[n+1]);}int main(){ int i,j,m,test,d1,d2,v,minn,tar; scanf("%d",&test); while(test--) { scanf("%d",&n); ans=0; for(i=0;i<=n+1;i++) { a[i].clear(); vis[i]=0; dis[i]=0; } for(i=1;i<=n;i++) { scanf("%d",&v); addEdge(0,i,v); addEdge(i,n+1,-v); } for(i=1;i<=n-1;i++) { scanf("%d%d%d",&d1,&d2,&v); addEdge(d1,d2,-v); addEdge(d2,d1,-v); } spfa(); } return 0;}
阅读全文
0 0
- HDU 6201 transaction transaction transaction(SPFA算法求最长路径)
- hdu 6201 transaction transaction transaction (spfa求最长路)
- HDU 6201 transaction transaction transaction【树形DP||SPFA最长路】
- 【建图+spfa算法】transaction transaction transaction HDU
- HDU 6201 transaction transaction transaction(SPFA模板)
- Hdu 6201 transaction transaction transaction【最长路】
- hdu 6201 transaction transaction transaction(最长路)
- HDU 6201 transaction transaction transaction (最长路)
- Hdu 6201 transaction transaction transaction(最长路)
- hdu 6201transaction transaction transaction
- HDU 6201 transaction transaction transaction
- hdu 6201 transaction transaction transaction
- HDU 6201 transaction transaction transaction
- hdu-6201 transaction transaction transaction
- hdu 6201 transaction transaction transaction
- 【最短路 spfa && 水题】hdu-6201 transaction transaction transaction
- transaction transaction transaction HDU
- transaction transaction transaction HDU
- 好的博客学习的地址【持续更新中】
- javascript函数
- UVa 202 循环小数节
- 计蒜客 Banana
- 02 Hibernate的几个类
- HDU 6201 transaction transaction transaction(SPFA算法求最长路径)
- 实现DotA的技能冷却效果
- Spring Boot入门之 HelloWorld
- Eureka的高可用实战:服务提供者、服务消费者集群之间的调用方式
- centos7安装出现license information(license not accepted)解决办法
- AI与游戏——吃豆人(3)基本的路径规划算法(上)
- 【干货】Apache Hadoop 2.8 完全分布式集群搭建超详细过程,实现NameNode HA、ResourceManager HA高可靠性
- HDU 1018 Big Number(数学知识)
- 自定义场景和场景跳转