I am solving this problem
https://www.acmicpc.net/problem/1238#
You can change the language by clicking the button
The idea I came up with is to find the sum of the shortest distance from the Kth to the second and the second to the Kth
so here is my whole source code
#include <stdio.h>
#define INF 999999
#define min(x,y) ((x)>(y)?(y):(x))
using namespace std;
int ans = 0;
int n,m,x;
int d[1001][1001];
void Floyd_Warshal(){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==j) d[i][j]=0;
}
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
void solve(){
for(int i=1; i<=n; i++){
if(i==2) continue;
if(d[i][2] + d[2][i]>ans) ans = d[i][2] + d[2][i];
//printf("%d = %d+%d \n",ans,d[i][2],d[2][i]);
}
}
int main(){
scanf("%d %d %d",&n,&m,&x);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) d[i][j] = INF;
}
for(int i=0; i<m; i++){
int u,v,t;
scanf("%d %d %d",&u,&v,&t);
d[u][v] = t;
}
Floyd_Warshal();
solve();
printf("%d\n",ans);
return 0;
}
I think Floyd_warshal() function is fine.
But, I guess I take a wrong approach(The idea suggested above) to solve the problem so I just wanna ask that my idea is right approach to solve the problem or not.
The complexity of the Floyd-Warshall algorithm is O(n3). It will be TL.
The solution to this task is to doing SSSP (single source shortest path). It can do effectively with Dijkstra's Algorithm. You can do Dijkstra's Algorithm on farm X then doing it again with edges reversed. It has O(m logn) complexity if you write it with priority queues or set. Google for this, there are many information, for example 1, 2.
After this your idea is right, you should take maximum of sum D[i][X] + D[i][X] for each farm i. Though you have written 2 instead of x, but it is probably a typo, or to test code.
Related
Firstly, a number N has to be input, and the matrix is of NxN dimensions then. The diagonal of the matrix has to be all 0, the elements of the matrix above the diagonal have to be from 1 to N diagonally, and the elements under the diagonal need to be filled from -1 to -N also diagonally. It should be something like this (if N=5):
But the problem that I have is that I print it out like this:
and I don't know how to fix it.
This is the code that I have:
`#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d",&N);
int k=0;
for(i=0; i<N; i++){
for(j=0; j<N; j++){
if(i==j){
matrix[i][j]=0;
}
else if(j>i && i!=j){
for(k=0; k<N; k++){
matrix[k-1][j]=k;
}
}
else if(j<i && i!=j){
for(k=0; k<N; k++){
matrix[i][k-1]=-k;
}
}
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}`
I would really appreciate the help.
Here is you code modified, notice that 3 inner loops are removed with only one line.
second, you ask for the number N, however due to statically initialisation to maximum 50, you should as well verify that it is not. otherwise segmentation fault will happen.
or if you want to allow N >50 then better to do dynamic allocation on matrix.
#include <stdio.h>
int main() {
int matrix[50][50], i, j, N;
printf("N: ");
scanf("%d", &N);
if (N > 50){
printf("N should be smaller than 50, \n");
N = 50;
}
for(i=0; i<N; i++){
for(j=0; j<N; j++){
matrix[i][j]= j - i;
}
}
printf("Matrix:\n");
for (i=0; i<N; i++) {
for (j=0; j<N; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
return 0;
}
Im trying to study some algorithm questions and I can't find a way to start my select sort algorithm code. Im using Visual Studio and the code doesn't show any errors through compiling. When I start the code the system just stops for a few seconds and print "press any key to continue...". Im a beginner and I can't see what's wrong. HELP MEEE
#include <stdio.h>
int n = 6;
int qwerty(int a[]) {
int i, j, t;
for (i=1; i<=n-1; i++){
for (j=i+1; j<=n; j++){
if (a[i] > a[j]){
t = a[j];
a[j] = a[i];
a[i] = t;
}
}
}
}
int main(void) {
int a[6] = { 2, 14, 20, 8, 17, 13 };
qwerty(a[n]);
int i;
for (i = 1; i <= n; i++){
printf("%d ", a[i]);
}
}
In the C language, arrays start indexing at 0, and the last element is at the index n-1. So the first thing you should change is the loops to be for(i = 0; i < n-1; i++) and for(j = i + 1; j < n; j++)
Also, this is a bubble sort algorithm, not selection sort. Selection sort algorithms can be found anywhere so I'm not going to type it here.
#include<stdio.h>
int main()
{
int i,j,k,n;
float A[20][20],c,x[10];
scanf("%d",&n);//size of matrix
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%f",&A[i][j]);
}
}
//looking for elements in the diagonal matrix
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%f\n",i,x[i]);//solution
}
return(0);
}
it prints things like "#IO". The algorithm is supposed to have three equations with n variables as inputs. I think the problem is when I divide by zero, but I don't know what can I add in order to avoid that.
I am trying to learn C and I am trying to write a piece of code which does the following:
Take user input of a natural number n
Take user input of n elements and store them in the array x
Delete all negative numbers from the array x
Print the new array, with the length n - number of deleted elements
Here is my code:
#include <stdio.h>
int main(void)
{
int n, i, count=0;
double x[1000];
scanf("%d", &n);
for (i=0; i<n; i++)
scanf("%lg", &x[i]);
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
x[i-count]=x[i];
};
n -= count;
for (i=0; i<n; i++)
printf("%d: %g\n", i, x[i]);
return 0;
}
I have been told that I should replace my second for loop with the following code:
int j=0
...
for (i=0; i<n; i++)
{
if (x[i] < 0)
{
count++;
continue;
};
if (i > j)
x[j] = x[i];
j++;
};
Could someone please explain why is the latter code better?
If i==j, then you're assigning an element to itself: not wrong, but a (small) waste of effort.
If you really want to improve this, avoid putting the negative values in the array in the first place.
I'm creating a sudoku solver in C and having trouble obtaining user input. The code that I've written doesn't input the data into the game board, but if I change Game_Buffer[counter] to Game_Buffer[i] it inputs the data but only 9 characters. I am aware of why. I just wanted to see if their were problems in other areas.
My primary question is: Why is the method I'm using not placing the user input data into the game board array?
#include <stdio.h>
#include <string.h>
#define CELL 81
int main()
{
// Banner
printf("\t\t\t\tSudoku Solver\n");
printf("\t\t\t***************************\n");
//initialize variables
char Game_Board[9][9];
int i,j;
char Game_Buffer[CELL];
int counter = 0;
printf("Please enter the numbers of the board * denotes a blank space\n");
fgets(Game_Buffer,CELL,stdin);
for(i=0;i<strlen(Game_Buffer);i++)
printf("%c", Game_Buffer[i]);
while(counter < 81)
{
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
printf("%d\n", counter);
printf("\t\t\t\t The Board\n");
for( i=0; i<9; i++)
for( j=0; j<9; j++)
{
if( j % 3 == 0)
printf("|");
printf("%c", Game_Board[i][j]);
if(j==8)
printf("|\n");
}
return 0;
}
You probably should use brackets at first place.
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
Game_Board [i][j] = Game_Buffer [counter];
counter++;
}
}
Add all missing brackets and check if your issue still exists.
The counter++ executes after the loop. I have idented the code to show what I mean..
for(i=0; i<9; i++)
for(j=0; j<9; j++)
Game_Board [i][j] = Game_Buffer [counter];
counter++;
You are updating all cells with the same value.