HDU 1224 Free DIY Tour(spfa求最长路+路径输出)
传送门:
http://acm.hdu.edu.cn/showproblem.php?pid=1224
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8692 Accepted Submission(s): 2804
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both 1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
Output a blank line between two cases.
3
0 70 90
4
1 2
1 3
2 4
3 4
3
0 90 70
4
1 2
1 3
2 4
3 4
points : 90
circuit : 1->3->1
CASE 2#
points : 90
circuit : 1->2->1
#include <iostream>
#include <cstdio>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<memory>
#include<queue>
using namespace std;
typedef long long LL;
#define max_v 120
int n,m;
int G[max_v][max_v];
int a[max_v];
int dis[max_v];//保存价值
int way[max_v];//保存路
void spfa(int s,int tn)
{
for(int i=;i<=tn;i++)
{
dis[i]=;
way[i]=;
}
queue<int>q;
q.push(s); int p;
while(!q.empty())
{
p=q.front();
q.pop(); for(int i=;i<=tn;i++)
{
if(G[p][i]!=)
{
if(dis[p]+a[i]>dis[i])
{
dis[i]=dis[p]+a[i];
way[i]=p;
q.push(i);
}
}
}
}
}
void pri(int tn)//路径打印
{
int a[];
int c=;
int i=tn;
while(way[i])
{
a[c++]=way[i];
i=way[i];
}
for(int i=c-;i>=;i--)
{
printf("%d->",a[i]);
}
printf("1\n");
}
int main()
{
int t;
scanf("%d",&t);
int k=;
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
a[n+]=;
scanf("%d",&m);
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
G[x][y]=;
}
spfa(,n+);
if(k!=)
printf("\n");
printf("CASE %d#\n",k++);
printf("points : %d\n",dis[n+]);
printf("circuit : ");
pri(n+);
}
return ;
}
最新文章
- AngularJs的UI组件ui-Bootstrap分享(十一)——Typeahead
- Vim快速移动光标至行首和行尾
- java事件监听
- Elasticsearch内存分配设置详解
- Struts2 - Convertion
- MS SQL SERVER 中的系统表
- MVC生命周期
- 74HC595
- [Socket]BSD Socket网络通信
- not exists 跟not in 纪念一下
- 《University Calculus》-chape5-积分法-微积分基本定理
- <; meta http-equiv = ";X-UA-Compatible"; content = ";IE=edge,chrome=1"; />;的作用
- Android多线程的研究(8)——Java5于Futrue获取线程返回结果
- Android从无知到有知——NO.4
- HC - 05 bluetooth module settings in Linux using CuteCom
- panel的autoscroll属性不起作用
- javaCV开发详解之6:本地音频(话筒设备)和视频(摄像头)抓取、混合并推送(录制)到服务器(本地)
- Solr集群搭建详细教程(一)
- maven项目 在eclipse,InteliJ IDEA中的一些问题
- Django环境安装--Django从入门到精通系列教程