全局变量(数组)开大了
本来想搞个dist【N】[N]
/tmp/cccMPc7i.o: In function `dijikstra(int)':
a.cpp:(.text+0xb3): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0xe3): relocation truncated to fit: R_X86_64_32S against symbol `vis' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x154): relocation truncated to fit: R_X86_64_PC32 against symbol `n' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x1a2): relocation truncated to fit: R_X86_64_32S against symbol `vis' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x1ba): relocation truncated to fit: R_X86_64_32S against symbol `vis' defined in .bss section in /tmp/cccMPc7i.o
/tmp/cccMPc7i.o: In function `dfs(int, int, int)':
a.cpp:(.text+0x378): relocation truncated to fit: R_X86_64_32S against symbol `st' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x38a): relocation truncated to fit: R_X86_64_32S against symbol `st' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x3f6): relocation truncated to fit: R_X86_64_32S against symbol `st' defined in .bss section in /tmp/cccMPc7i.o
/tmp/cccMPc7i.o: In function `main':
a.cpp:(.text+0x419): relocation truncated to fit: R_X86_64_32 against symbol `n' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x428): relocation truncated to fit: R_X86_64_32 against symbol `m' defined in .bss section in /tmp/cccMPc7i.o
a.cpp:(.text+0x46f): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status
#include <iostream>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
const int N=5e4+10;
const int M=1e5+5;
int id[10];
struct edge{
int to,nxt,w;
}e[M<<1];
int head[N];
int cnt;
void add(int u,int v,int w){
e[++cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int dist[10][N];
bool vis[N];
int n,m;
void dijikstra(int s){
for(int i=1;i<=n;i++){
dist[s][i]=inf;
vis[i]=false;
}
priority_queue<pii, vector<pii> ,greater<pii> > Q;
Q.push({0,s});
dist[s][s]=0;
for(int k=0;k<n;k++){
if(Q.empty())break;
pii t=Q.top(); Q.pop();
int v=t.second;
if(vis[v]){
k--;
continue;
}
vis[v]=1;
for(int i=head[v];i;i=e[i].nxt){
int to=e[i].to;
int w=e[i].w;
if(dist[s][to]>dist[s][v]+w){
dist[s][to]=dist[s][v]+w;
Q.push({dist[s][to],to});
}
}
}
}
bool st[10];
int dfs(int p,int station,int dis){
if(station==5)return dis;
int res=inf;
for(int i=1;i<=5;i++){
if(!st[i]){
st[i]=true;
res=min(res,dfs(id[i],station+1,dis+dist[p][id[i]]));
st[i]=false;
}
}
return res;
}
int main(int argc, char** argv) {
cin>>n>>m;
id[0]=1;//佳佳家
for(int i=1;i<=5;i++)cin>>id[i];
int u,v,w;
while(m--){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
for(int i=0;i<=5;i++)dijikstra(id[i]);
cout<<dfs(1,0,0); //此时在何地,到了多少个车站 ,已经花了多少时间
return 0;
}
在这个样例出现 Segmentation Fault
11 15
11 4 9 10 8
1 3 7
1 5 5
1 4 10
1 2 6
9 5 3
5 6 8
6 3 7
3 2 2
2 8 5
8 4 8
3 8 8
2 7 11
6 10 20
10 7 12
4 11 30
原来是dist[][]存的是站点到站点,站点编号可不止10
只能存 必访站点的编号(0~5)才合理且不超内存
#include <iostream>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
const int N=5e4+10;
const int M=1e5+5;
int id[10];
struct edge{
int to,nxt,w;
}e[M<<1];
int head[N];
int cnt;
void add(int u,int v,int w){
e[++cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt;
}
int dist[10][N];
bool vis[N];
int n,m;
void dijikstra(int s,int sg){
for(int i=1;i<=n;i++){
dist[sg][i]=inf;
vis[i]=false;
}
priority_queue<pii, vector<pii> ,greater<pii> > Q;
Q.push({0,s});
dist[sg][s]=0;
for(int k=0;k<n;k++){
if(Q.empty())break;
pii t=Q.top(); Q.pop();
int v=t.second;
if(vis[v]){
k--;
continue;
}
vis[v]=1;
for(int i=head[v];i;i=e[i].nxt){
int to=e[i].to;
int w=e[i].w;
if(dist[sg][to]>dist[sg][v]+w){
dist[sg][to]=dist[sg][v]+w;
Q.push({dist[sg][to],to});
}
}
}
}
bool st[10];
int dfs(int p,int station,int dis){
if(station==5)return dis;
int res=inf;
for(int i=1;i<=5;i++){
if(!st[i]){
st[i]=true;
res=min(res,dfs(i,station+1,dis+dist[p][id[i]]));
st[i]=false;
}
}
return res;
}
int main(int argc, char** argv) {
cin>>n>>m;
id[0]=1;//佳佳家
for(int i=1;i<=5;i++)cin>>id[i];
int u,v,w;
while(m--){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,w);
}
for(int i=0;i<=5;i++)dijikstra(id[i],i);
cout<<dfs(0,0,0); //此时在6个站(0~5)的第几个,到了多少个车站 ,已经花了多少时间
return 0;
}