Warnings on too-small array sizes to functions in C++ I\'m running through a war
ID: 3775224 • Letter: W
Question
Warnings on too-small array sizes to functions in C++
I'm running through a warning after compiling my code
"
"
I still don't understand what's the main issue to this problem, is it the problem that the arrays are too small hence the time outs?
Input:
Output:
8
Here's My code:
#include
#include
#include
#include
#include
using namespace std;
map nameToNumber;
int graph[101][101];
bool reachable(int src, int target, int par = -1) {
if (src == target) return true;
bool isReachable = false;
for (int i = 0; i < 100; ++i) {
if (i != par && graph[src][i] != 0 && reachable(i, target, src)) {
isReachable = true;
}
}
return isReachable;
}
int main() {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
string name; cin >> name;
nameToNumber[name] = i;
}
vector > > edges;
for (int i = 0; i < m; ++i) {
string u, v; int w;
cin >> u >> v >> w;
edges.push_back(make_pair(w, make_pair(nameToNumber[v], nameToNumber[u])));
}
sort(edges.begin(), edges.end());
int cost = 0;
for (int i = 0; i < edges.size(); ++i) {
int u = edges[i].second.first;
int v = edges[i].second.second;
int w = edges[i].first;
if (!reachable(u, v)) {
graph[u][v] = 1;
cost += w;
}
}
cout << cost ;
return 0;
}
Explanation / Answer
There are some errors. First header files. Include them.
Next when declaring the vector edges, the declaration should be vector<int> edges. Correct it.
Next when declaring a map, the correct syntax is
map<int,int> mymap;
this is an example , you can use any data type in there.
Consider these mistakes.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.