# Help required in free ticket

Someone please help me optimize my code as I am getting sigsegv error in many cases and TLE in one case
The problem code link is below
https://www.codechef.com/INOIPRAC/problems/INOI1402/

#include<stdio.h>
#include<stdlib.h>
#define INF 32767
int i,j;
int EXTRACT_MIN(int nv,int q[],int d[])
{
int min=INF,pos;
for(i=0;i<nv;i++)
{
if(q[i]!=1 && d[i]<min)
{
min=d[i];
pos=i;
}
}
q[pos]=1;
return pos;
}
int isnotempty(int nv,int q[])
{
int n=0;
for(i=0;i<nv;i++)
if(q[i]==0)
n=1;
return n;
}
void dijkstra(int weight[][20],int r,int nv)
{
int d[20],pi[20],u,v,q[20]={0},cost=0;
for(i=0;i<nv;i++)
{
d[i]=INF;
pi[i]=-1;
}
d[r-1]=0;
while(isnotempty(nv,q))
{
u=EXTRACT_MIN(nv,q,d);
for(v=0;v<nv;v++)
if(weight[u][v]!=-1 && q[v]==0)
if( (weight[u][v]+d[u]) < d[v])
{
d[v]=weight[u][v]+d[u];
pi[v]=u;
}
}
for(i=0;i<nv;i++)
if(i!=r-1)
{
if(i=nv-1)
printf("%d",d[i]);
j=i;
do
{
j=pi[j];
}while(j!=r-1);
}
}
int main()
{
int nv,ne,weight[20][20],e1,e2,cost,r;
printf("");
scanf("%d %d",&nv,&ne);
for(i=0;i<20;i++)
for(j=0;j<20;j++)
weight[i][j]=-1;
for(i=0;i<ne;i++)
{
printf("");
scanf("%d %d %d",&e1,&e2,&cost);
weight[e1-1][e2-1]=weight[e2-1][e1-1]=cost;
}
r=1;
dijkstra(weight,r,nv);
return 0;

}