答案1
您可以使用label
状态之外的文本,并使用节点来标记边。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,arrows.meta}
\tikzset{%
node distance=2cm,
State/.style={%
draw,circle,
thick,
color=black!60!green,
inner sep=0pt,
minimum size=10mm,
fill=blue!40!green,
text=white,
},
StateMark/.style={%
State,
fill=black},
Edge/.style={%
color=blue!40!green,
very thick,
-{Triangle[scale=1.1]},%% Try Latex instead oc Triangle
text=black,
},
EdgeMark/.style={%
Edge,
color=blue!90!green,
text=black,
}
}
\begin{document}
\begin{tikzpicture}
\node[StateMark,label=90:r] (S1) {$\infty$};
\node[StateMark,label=90:s,right=of S1] (S2){0};
\node[State,label=90:t,right=of S2] (S3){2};
\node[State,label=90:x,right=of S3] (S4){6};
%%
\draw[Edge] (S1) -- node[above,pos=0.5]{5} (S2);
\draw[Edge] (S1) to[bend right] node[below,pos=0.5]{5} (S3);%% Default bend = 30 degrees
\draw[EdgeMark] (S2) -- node[above,pos=0.5]{2} (S3);
\draw[EdgeMark] (S2) to[bend left=40] node[above,pos=0.5]{5} (S4);
\draw[Edge] (S3) -- node[above,pos=0.5]{7} (S4);
\end{tikzpicture}
\end{document}
State
为了简化代码,可以通过将定义更改为将标签文本作为输入
State/.style={%
draw,circle,
thick,
color=black!60!green,
inner sep=0pt,
minimum size=10mm,
fill=blue!40!green,
text=white,
label=90:#1,
},
StateMark/.style={%
State=#1,
fill=black},
然后绘制状态为
\node[StateMark=r] (S1) {$\infty$};
\node[StateMark=s,right=of S1] (S2){0};
\node[State=t,right=of S2] (S3){2};
\node[State=x,right=of S3] (S4){6};