Pat(Advanced Level)Practice--1087(All Roads Lead to Rome)
Pat1087 코드
제목 설명:
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.
Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.
Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".
Sample Input:
6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
AC 코드:
dijkstra+DFS #include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#define MAXN 205
#define MAX 0x0FFFFFFF
using namespace std;
int n,k;
int matrix[MAXN][MAXN];
string name[MAXN];
int happiness[MAXN];
map<string,int> m;
int dis[MAXN];
int visited[MAXN];
int sumHapp=0,avgHapp=0,routeSum=0,shortest=MAX;
vector<int> resPath;
void dijkstra(int s){
int i,j,k,temp;
for(int i=0;i<n;i++){
dis[i]=matrix[s][i];
visited[i]=0;
}
dis[s]=0;
visited[s]=1;
for(int i=1;i<n;i++){
temp=MAX;
k=s;
for(int j=0;j<n;j++){
if(!visited[j]&&dis[j]<temp){
temp=dis[j];
k=j;
}
}
visited[k]=1;
for(int j=0;j<n;j++){
if(!visited[j]&&dis[k]+matrix[k][j]<dis[j]){
dis[j]=dis[k]+matrix[k][j];
}
}
}
}
void DFS(int start,int end,vector<int> &path,int hapSum,int pathSum){
int i,j;
if(pathSum>shortest)
return;
if(start==end){
if(pathSum>shortest)
return;
routeSum++;
if(hapSum<sumHapp)
return;
int avg=hapSum/(path.size()-1);
if(avg>avgHapp){
resPath=path;
avgHapp=avg;
sumHapp=hapSum;
}
return;
}
for(int i=0;i<n;i++){
if(!visited[i]&&matrix[start][i]!=MAX){
path.push_back(i);
visited[i]=1;
DFS(i,end,path,hapSum+happiness[i],pathSum+matrix[start][i]);
path.pop_back();
visited[i]=0;
}
}
}
int main(int argc,char *argv[]){
int dest;
string city;
cin>>n>>k>>city;
name[0]=city;
m[city]=0;
happiness[0]=0;
for(int i=1;i<n;i++){
cin>>name[i]>>happiness[i];
if(name[i].compare("ROM")==0){
dest=i;
}
m[name[i]]=i;
}
for(int i=0;i<n;i++){
dis[i]=MAX;
for(int j=0;j<n;j++){
matrix[i][j]=MAX;
}
}
string city1,city2;
int cost;
for(int i=0;i<k;i++){
cin>>city1>>city2>>cost;
int r=m[city1];
int c=m[city2];
if(cost<matrix[r][c]){
matrix[r][c]=matrix[c][r]=cost;
}
}
dijkstra(0);
for(int i=0;i<n;i++){
visited[i]=0;
}
shortest=dis[dest];
vector<int> path(1,0);
visited[0]=1;
DFS(0,dest,path,0,0);
printf("%d %d %d %d
",routeSum,shortest,sumHapp,avgHapp);
cout<<city;
for(int i=1;i<resPath.size();i++){
cout<<"->"<<name[resPath[i]];
}
cout<<endl;
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[BOJ] 5568 카드 놓기
아이디어
Level이 0일 때, 즉 아직 카드를 고르지 않았을 때 StringBuilder를 생성하고
sb에 고른 카드를 담도록 하였다.
이후 해당 노드 탐색을 종료하면 sb에 담은 카드를 삭제해 주었다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#define MAXN 205
#define MAX 0x0FFFFFFF
using namespace std;
int n,k;
int matrix[MAXN][MAXN];
string name[MAXN];
int happiness[MAXN];
map<string,int> m;
int dis[MAXN];
int visited[MAXN];
int sumHapp=0,avgHapp=0,routeSum=0,shortest=MAX;
vector<int> resPath;
void dijkstra(int s){
int i,j,k,temp;
for(int i=0;i<n;i++){
dis[i]=matrix[s][i];
visited[i]=0;
}
dis[s]=0;
visited[s]=1;
for(int i=1;i<n;i++){
temp=MAX;
k=s;
for(int j=0;j<n;j++){
if(!visited[j]&&dis[j]<temp){
temp=dis[j];
k=j;
}
}
visited[k]=1;
for(int j=0;j<n;j++){
if(!visited[j]&&dis[k]+matrix[k][j]<dis[j]){
dis[j]=dis[k]+matrix[k][j];
}
}
}
}
void DFS(int start,int end,vector<int> &path,int hapSum,int pathSum){
int i,j;
if(pathSum>shortest)
return;
if(start==end){
if(pathSum>shortest)
return;
routeSum++;
if(hapSum<sumHapp)
return;
int avg=hapSum/(path.size()-1);
if(avg>avgHapp){
resPath=path;
avgHapp=avg;
sumHapp=hapSum;
}
return;
}
for(int i=0;i<n;i++){
if(!visited[i]&&matrix[start][i]!=MAX){
path.push_back(i);
visited[i]=1;
DFS(i,end,path,hapSum+happiness[i],pathSum+matrix[start][i]);
path.pop_back();
visited[i]=0;
}
}
}
int main(int argc,char *argv[]){
int dest;
string city;
cin>>n>>k>>city;
name[0]=city;
m[city]=0;
happiness[0]=0;
for(int i=1;i<n;i++){
cin>>name[i]>>happiness[i];
if(name[i].compare("ROM")==0){
dest=i;
}
m[name[i]]=i;
}
for(int i=0;i<n;i++){
dis[i]=MAX;
for(int j=0;j<n;j++){
matrix[i][j]=MAX;
}
}
string city1,city2;
int cost;
for(int i=0;i<k;i++){
cin>>city1>>city2>>cost;
int r=m[city1];
int c=m[city2];
if(cost<matrix[r][c]){
matrix[r][c]=matrix[c][r]=cost;
}
}
dijkstra(0);
for(int i=0;i<n;i++){
visited[i]=0;
}
shortest=dis[dest];
vector<int> path(1,0);
visited[0]=1;
DFS(0,dest,path,0,0);
printf("%d %d %d %d
",routeSum,shortest,sumHapp,avgHapp);
cout<<city;
for(int i=1;i<resPath.size();i++){
cout<<"->"<<name[resPath[i]];
}
cout<<endl;
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[BOJ] 5568 카드 놓기아이디어 Level이 0일 때, 즉 아직 카드를 고르지 않았을 때 StringBuilder를 생성하고 sb에 고른 카드를 담도록 하였다. 이후 해당 노드 탐색을 종료하면 sb에 담은 카드를 삭제해 주었다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.