포인터로 넣으면 변경시 어차피 본체가 갱신되는데 굳이 return값으로 받으려는 이유가 있나요?
다익스트라 돌리고 erase함수에서 지우는 걸로 보아 간선리스트는 dijkstra()에서 변경할 거 같진 않은데요.
굳이 리턴값으로 받고 싶으시면
vector<vector< pair<int, int > >> adj(n + 1);
이렇게 선언하시고
vector<vector<pair<int, int>>> dijkstra(vector<vector<pair<int, int>>> adj)
{
priority_queue<pair<int, int > > pq;
pq.push(make_pair(0, s));
while (!pq.empty())
{
int here = pq.top().second;
int cost = -pq.top().first;
pq.pop();
for (int i = 0; i < adj[here].size(); i++)
{
if (dist[adj[here][i].first] != -1) continue;
if (dist[adj[here][i].second] == -1) continue;
pq.push(make_pair(-cost - dist[adj[here][i].second], dist[adj[here][i].first]));
}
}
return adj;
}
이렇게 하시면 될 거 같네요. 아니면
void dijkstra(vector<vector<pair<int, int>>> &adj)
{
priority_queue<pair<int, int > > pq;
pq.push(make_pair(0, s));
while (!pq.empty())
{
int here = pq.top().second;
int cost = -pq.top().first;
pq.pop();
for (int i = 0; i < adj[here].size(); i++)
{
if (dist[adj[here][i].first] != -1) continue;
if (dist[adj[here][i].second] == -1) continue;
pq.push(make_pair(-cost - dist[adj[here][i].second], dist[adj[here][i].first]));
}
}
}
이렇게 해서 adj 자체를 바꿔버리는 방법도 있구요.
자유 카테고리면 코드 입력이 불편해요....
kyaryunha 6년 전
5719 번 풀고 있긴 한데, 문제에 대한 질문과는 멀어서 카테고리는 자유로..!
아래는 짜다말은 코드인데요!
궁금한 것 있어서 질문 올립니다..! :)
코드에서 글씨 굵기 굵게 해놓은 부분 보면, 하고 싶은건
main 함수에서 vector<pair<int,int>>로 선언한 함수를,
dijkstra() 함수에서도 쓰고 싶은데, 그리고 다시 쓰고 나서 변환한 값에 대해 다시 main 함수로 넘겨주려고 하는데,
단순한 변수나 배열과는 달리 vector은 저렇게 하면 안되는지 에러가 뜹니다..!
혹시 vector<pair<int,int>>를 주고받으려면 어떻게 해야 하는지 알려주실 분 계실까요??
/*전역변수로 선언하는 방법도 있겠지만, 그거 말고 다른 방법 알고 싶어요..!*/
#include<stdio.h>
#include<memory.h>
#include<vector>
#include<queue>
#include<utility>
using namespace std;
int n,m,x,y,z,s,e;
int dist[505];
vector<pair<int,int > > dijkstra(vector<pair<int,int > > &adj );
vector<pair<int,int > > erase(vector<pair<int,int > > &adj );
int main(void)
{
for(;;)
{
scanf("%d %d",&n,&m);
vector< pair<int,int > > adj[n+1];
if(n==0&&m==0) break;
scanf("%d %d",&s,&e);
memset(dist,-1,sizeof(dist)); dist[s]=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d %d",&x,&y,&z);
adj[x].push_back(pair<int ,int>(y,z));
}
dijkstra(adj);
erase(adj);
dijkstra(adj);
printf("%d",dist[e]);
}
}
vector<pair<int,int > > dijkstra(vector<pair<int,int > > &adj )
{
priority_queue<pair<int, int > > pq;
pq.push(make_pair(0,s));
while(!pq.empty())
{
int here = pq.top().second();
int cost = -pq.top().first();
pq.pop();
for(int i=0;i<adj[here].size();i++)
{
if( dist[adj[here][i].first()] != -1 ) continue;
if( dist[adj[here][i].second()] == -1 ) continue;
pq.push(make_pair(-cost-dist[adj[here][i].second()], dist[adj[here][i].first()] ));
}
}
return adj;
}
vector<pair<int,int > > erase(vector<pair<int,int > > &adj )
{
}